<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\report\kuaishou;

use app\enum\ReportRecordEnum;
use app\model\ClientAccountModel;
use app\model\ClientAccountOperationTimeModel;
use app\model\ReportRecordPushKsAccountDateModel;
use app\model\ReportRecordPushKsAccountModel;
use app\model\ReportRecordPushKsCampaignModel;
use app\model\ReportRecordPushKsUnitModel;
use app\service\report\ReportService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

final class KsProcessingDataService extends ReportService
{
    /**
     * 数据处理.
     */
    public static function processingData(array $params = [])
    {
        try {
            $data = [];
            $result = [];
            $dataArr = [];
            $totalArr = [];
            // 是否启用汇总数据：0不启用 1启用
            $is_sum = $params['is_sum'];
            // 保留历史数据(只用于时报)：0不勾选 1勾选
            $is_retain = $params['is_retain'] ?? 0;
            // 报表要导出的字段
            $add_fields = json_decode($params['add_fields'], true);
            // 发送的时间点
            $sendTime = $params['sendTime'];
            // 发送的日期
            $sendDate = $params['sendDate'];
            $sendYesterdayDate = strtotime(strval($sendDate)) - 86400;
            $sendBeforeYesterdayDate = strtotime(strval($sendDate)) - (2 * 86400);
            // 发送的时间点数组
            $send_time = json_decode($params['send_time'], true);
            // 数据维度 1账户 2广告组 3广告计划
            $dimension = $params['dimension'];
            // 报表排序 1账户 2消耗
            $dataSort = $params['data_sort'];
            // 报表类型：1时报；2日报 (日报就是昨天的所有的数据，时报就是当天截止到时间点的数据)
            $report_date_type = $params['report_date_type'];
            // 折算比例
            $cash_cost_ratio = $params['cash_cost_ratio'];
            // 折后成本-折算比例
            $folding_convert_ratio = $params['folding_convert_ratio'];
            // 折后成本-选择的字段
            $folding_convert_field = $params['folding_convert_field'];
            // 选择的账户ID数组
            $advertiserIds = json_decode($params['adv_unit_plan_lists'], true) ?? [];
            // 自定义数据数组
            $custom_fields_name = $params['custom_fields_name'] ? json_decode($params['custom_fields_name'], true) : [];
            // 获取所有广告主的信息
            $advInfos = [];
            $productInfos = [];
            $advInfoss = (new ClientAccountModel())->alias('a')
                ->leftJoin(['habo_account_port' => 'b'], 'a.advertiser_id=b.advertiser_id')
                ->leftJoin(['habo_client_promotion_product' => 'c'], 'c.id=b.product_id')
                ->whereIn('a.advertiser_id', $advertiserIds)
                ->field('a.advertiser_id,a.account_name,c.product_name')
                ->select()
                ->toArray();
            if (! empty($advInfoss)) {
                foreach ($advInfoss as $val) {
                    $advInfos[$val['advertiser_id']] = $val['account_name'];
                    $productInfos[$val['advertiser_id']] = $val['product_name'];
                }
            }
            $yesterdayEventNextDayStayArr = [];
            $yesterdayChargeArr = [];
            $yesterdayActivationArr = [];
            $ciDayEventNextDayStayArr = [];
            $ciDayChargeArr = [];
            $ciDayActivationArr = [];
            $advertiserKey = 0;
            foreach ($advertiserIds as $advertiserId) {
                $eventNextDayStay = 0;
                $eventNextDayStayCharge = 0;
                $eventNextDayStayActivation = 0;
                $eventNextDayStayCost = 0;
                $eventNextDayStayRatio = 0;
                $yesterdayEventNextDayStay = 0;
                $yesterdayCharge = 0;
                $yesterdayActivation = 0;
                $yesterdayEventNextDayStayCost = 0;
                $yesterdayEventNextDayStayRatio = 0;
                // 次日次留
                if (in_array('event_next_day_stay', $add_fields) || in_array('event_next_day_stay_cost', $add_fields) || in_array('event_next_day_stay_ratio', $add_fields)) {
                    $statDate = date('Ymd', $sendYesterdayDate);
                    // 获取报表数据
                    switch ($dimension) {
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_ACCOUNT:
                            $list = self::getAccountSecondaryStation(strval($advertiserId), intval($statDate), intval($sendTime), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_UNIT:
                            $list = self::getCampaignSecondaryStation(strval($advertiserId), intval($statDate), intval($sendTime), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_CAMPAIGN:
                            $list = self::getUnitSecondaryStation(strval($advertiserId), intval($statDate), intval($sendTime), intval($report_date_type));
                            break;
                        default:
                            return self::setError('获取快手媒体数据失败');
                    }
                    // 判断当前页码是否获取全部数据
                    if (! empty($list)) {
                        // 次日次留数
                        $eventNextDayStay = $list['event_next_day_stay'];
                        $eventNextDayStayCharge = $list['charge'];
                        $eventNextDayStayActivation = $list['activation'];
                        $eventNextDayStayCost = ! empty($eventNextDayStay) ? round($eventNextDayStayCharge / $eventNextDayStay, 3) : 0;
                        $eventNextDayStayRatio = ! empty($eventNextDayStayActivation) ? round($eventNextDayStay / $eventNextDayStayActivation * 100, 2) . '%' : 0;
                        $ciDayEventNextDayStayArr[] = $eventNextDayStay;
                        $ciDayChargeArr[] = $eventNextDayStayCharge;
                        $ciDayActivationArr[] = $eventNextDayStayActivation;
                        unset($list);
                    }
                }
                // 昨日次留
                if (in_array('yesterday_event_next_day_stay', $add_fields) || in_array('yesterday_event_next_day_stay_cost', $add_fields) || in_array('yesterday_event_next_day_stay_ratio', $add_fields)) {
                    $statDate = date('Ymd', $sendBeforeYesterdayDate);
                    // 获取报表数据
                    switch ($dimension) {
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_ACCOUNT:
                            $list = self::importAccountReportDate(intval($statDate), strval($advertiserId), intval($report_date_type), intval($sendTime));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_UNIT:
                            $list = self::importCampaignReport(intval($statDate), strval($advertiserId), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_CAMPAIGN:
                            $list = self::importUnitReport(intval($statDate), strval($advertiserId), intval($report_date_type));
                            break;
                        default:
                            return self::setError('获取快手媒体数据失败');
                    }
                    // 判断当前页码是否获取全部数据
                    if (! empty($list)) {
                        // 昨日次留数
                        $yesterdayEventNextDayStay = $list[0]['event_next_day_stay'];
                        $yesterdayCharge = $list[0]['charge'];
                        $yesterdayActivation = $list[0]['activation'];
                        $yesterdayEventNextDayStayCost = ! empty($yesterdayEventNextDayStay) ? round($yesterdayCharge / $yesterdayEventNextDayStay, 3) : 0;
                        $yesterdayEventNextDayStayRatio = ! empty($yesterdayActivation) ? round($yesterdayEventNextDayStay / $yesterdayActivation * 100, 2) . '%' : 0;
                        $yesterdayEventNextDayStayArr[] = $yesterdayEventNextDayStay;
                        $yesterdayChargeArr[] = $yesterdayCharge;
                        $yesterdayActivationArr[] = $yesterdayActivation;
                        unset($list);
                    }
                }
                if ($report_date_type == 1) {
                    $statDate = $sendDate;
                    // 获取时报报表数据
                    switch ($dimension) {
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_ACCOUNT:
                            $list = self::importAccountReport(intval($statDate), strval($advertiserId), intval($report_date_type), intval($sendTime));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_UNIT:
                            $list = self::importCampaignReport(intval($statDate), strval($advertiserId), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_CAMPAIGN:
                            $list = self::importUnitReport(intval($statDate), strval($advertiserId), intval($report_date_type));
                            break;
                        default:
                            return self::setError('获取快手媒体数据失败');
                    }
                } else {
                    $statDate = date('Ymd', $sendYesterdayDate);
                    // 获取日报报表数据
                    switch ($dimension) {
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_ACCOUNT:
                            $list = self::importAccountReportDate(intval($statDate), strval($advertiserId), intval($report_date_type), intval($sendTime));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_UNIT:
                            $list = self::importCampaignReport(intval($statDate), strval($advertiserId), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_CAMPAIGN:
                            $list = self::importUnitReport(intval($statDate), strval($advertiserId), intval($report_date_type));
                            break;
                        default:
                            return self::setError('获取快手媒体数据失败');
                    }
                }

                $send_times = [];
                foreach ($send_time as $stk => $stv) {
                    if (strtotime($sendDate . ' ' . $stv . ':00') <= strtotime($sendDate . ' ' . $sendTime . ':00')) {
                        $send_times[$stk] = strtotime($sendDate . ' ' . $stv . ':00');
                    }
                }
                sort($send_times);
                if (! empty($list)) {
                    // 日报需要计算0点到当前点的合计
                    $tmpArr = [];
                    if ($report_date_type == 1) {
                        // 保留历史数据
                        if ($is_retain == 1) {
                            foreach ($send_times as $stv) {
                                foreach ($list as $val) {
                                    // 小于当前时间点的数据拼入数组
                                    if ($stv > strtotime($val['stat_date'] . ' ' . $val['stat_hour'] . ':00:00') && $stv <= strtotime($sendDate . ' ' . $sendTime . ':00')) {
                                        if ($dimension == 1) {
                                            $tmpArr[$stv . '_' . $advertiserId][] = $val;
                                        } elseif ($dimension == 3) {
                                            $tmpArr[$stv . '_' . $val['campaign_id']][] = $val;
                                        } elseif ($dimension == 2) {
                                            $tmpArr[$stv . '_' . $val['unit_id']][] = $val;
                                        }
                                    }
                                }
                            }
                        } else {
                            $stv = strtotime($sendDate . ' ' . $sendTime . ':00');
                            foreach ($list as $val) {
                                // 小于当前时间点的数据拼入数组
                                if ($stv >= strtotime($val['stat_date'] . ' ' . $val['stat_hour'] . ':00:00')) {
                                    if ($dimension == 1) {
                                        $tmpArr[$stv . '_' . $advertiserId][] = $val;
                                    } elseif ($dimension == 3) {
                                        $tmpArr[$stv . '_' . $val['campaign_id']][] = $val;
                                    } elseif ($dimension == 2) {
                                        $tmpArr[$stv . '_' . $val['unit_id']][] = $val;
                                    }
                                }
                            }
                        }
                        unset($list);
                        $list = [];
                    }
                    if (! empty($tmpArr)) {
                        $i = 0;
                        foreach ($tmpArr as $tak => $tav) {
                            $list[$i]['charge'] = array_sum(array_column($tav, 'charge'));
                            $list[$i]['show'] = array_sum(array_column($tav, 'show'));
                            $list[$i]['photo_click'] = array_sum(array_column($tav, 'photo_click'));
                            $list[$i]['aclick'] = array_sum(array_column($tav, 'aclick'));
                            $list[$i]['bclick'] = array_sum(array_column($tav, 'bclick'));
                            $list[$i]['photo_click_ratio'] = ! empty($list[$i]['show']) ? round($list[$i]['photo_click'] / $list[$i]['show'] * 100, 2) . '%' : 0;
                            $list[$i]['aclick_ratio'] = ! empty($list[$i]['aclick']) ? round($list[$i]['bclick'] / $list[$i]['aclick'] * 100, 2) . '%' : 0;
                            $list[$i]['cash_cost'] = array_sum(array_column($tav, 'cash_cost'));
                            $list[$i]['yesterday_cost'] = array_sum(array_column($tav, 'yesterday_cost'));
                            $list[$i]['show_1k_cost'] = ! empty($list[$i]['show']) ? round($list[$i]['charge'] / $list[$i]['show'] * 1000, 3) : 0;
                            $list[$i]['aclick_1k_cost'] = ! empty($list[$i]['aclick']) ? round($list[$i]['charge'] / $list[$i]['aclick'] * 1000, 3) : 0;
                            $list[$i]['photo_click_cost'] = ! empty($list[$i]['photo_click']) ? round($list[$i]['charge'] / $list[$i]['photo_click'], 3) : 0;
                            $list[$i]['action_cost'] = ! empty($list[$i]['bclick']) ? round($list[$i]['charge'] / $list[$i]['bclick'], 3) : 0;
                            $list[$i]['download_started'] = array_sum(array_column($tav, 'download_started'));
                            $list[$i]['download_started_cost'] = ! empty($list[$i]['download_started']) ? round($list[$i]['charge'] / $list[$i]['download_started'], 3) : 0;
                            $list[$i]['download_started_ratio'] = ! empty($list[$i]['bclick']) ? round($list[$i]['download_started'] / $list[$i]['bclick'] * 100, 2) . '%' : 0;
                            $list[$i]['download_completed'] = array_sum(array_column($tav, 'download_completed'));
                            $list[$i]['download_completed_cost'] = ! empty($list[$i]['download_completed']) ? round($list[$i]['charge'] / $list[$i]['download_completed'], 3) : 0;
                            $list[$i]['download_completed_ratio'] = ! empty($list[$i]['download_started']) ? round($list[$i]['download_completed'] / $list[$i]['download_started'] * 100, 2) . '%' : 0;
                            $list[$i]['activation'] = array_sum(array_column($tav, 'activation'));
                            $list[$i]['conversion_cost'] = ! empty($list[$i]['activation']) ? round($list[$i]['charge'] / $list[$i]['activation'], 3) : 0;
                            $list[$i]['key_action'] = array_sum(array_column($tav, 'key_action'));
                            $list[$i]['key_action_cost'] = ! empty($list[$i]['key_action']) ? round($list[$i]['charge'] / $list[$i]['key_action'], 3) : 0;
                            $list[$i]['key_action_ratio'] = ! empty($list[$i]['activation']) ? round($list[$i]['key_action'] / $list[$i]['activation'] * 100, 2) . '%' : 0;
                            $list[$i]['download_conversion_ratio'] = ! empty($list[$i]['download_completed']) ? round($list[$i]['activation'] / $list[$i]['download_completed'] * 100, 2) . '%' : 0;
                            $list[$i]['event_register'] = array_sum(array_column($tav, 'event_register'));
                            $list[$i]['event_register_cost'] = ! empty($list[$i]['event_register']) ? round($list[$i]['charge'] / $list[$i]['event_register'], 3) : 0;
                            $list[$i]['event_register_ratio'] = ! empty($list[$i]['activation']) ? round($list[$i]['event_register'] / $list[$i]['activation'] * 100, 2) . '%' : 0;
                            $list[$i]['event_pay_first_day'] = array_sum(array_column($tav, 'event_pay_first_day'));
                            $list[$i]['event_pay_purchase_amount_first_day'] = array_sum(array_column($tav, 'event_pay_purchase_amount_first_day'));
                            $list[$i]['event_pay_first_day_cost'] = ! empty($list[$i]['event_pay_first_day']) ? round($list[$i]['charge'] / $list[$i]['event_pay_first_day'], 3) : 0;
                            $list[$i]['event_pay_first_day_roi'] = array_sum(array_column($tav, 'event_pay_first_day_roi'));
                            $list[$i]['event_pay'] = array_sum(array_column($tav, 'event_pay'));
                            $list[$i]['event_pay_purchase_amount'] = array_sum(array_column($tav, 'event_pay_purchase_amount'));
                            $list[$i]['event_pay_cost'] = ! empty($list[$i]['event_pay']) ? round($list[$i]['charge'] / $list[$i]['event_pay'], 3) : 0;
                            $list[$i]['event_pay_roi'] = ! empty($list[$i]['charge']) ? round($list[$i]['event_pay_purchase_amount'] / $list[$i]['charge'], 3) : 0;
                            // $list[$i]['event_next_day_stay'] = array_sum(array_column($tav, 'event_next_day_stay'));
                            // $list[$i]['event_next_day_stay_cost'] = !empty($list[$i]['event_next_day_stay'])?round($list[$i]['charge']/$list[$i]['event_next_day_stay'], 3):0;
                            // $list[$i]['event_next_day_stay_ratio'] = !empty($list[$i]['activation'])?round($list[$i]['event_next_day_stay']/$list[$i]['activation']*100, 2).'%':0;
                            $list[$i]['event_next_day_stay'] = ! empty($eventNextDayStay) ? $eventNextDayStay : 0;
                            $list[$i]['event_next_day_stay_cost'] = ! empty($eventNextDayStayCost) ? $eventNextDayStayCost : 0;
                            $list[$i]['event_next_day_stay_ratio'] = ! empty($eventNextDayStayRatio) ? $eventNextDayStayRatio : 0;
                            $list[$i]['yesterday_event_next_day_stay'] = ! empty($yesterdayEventNextDayStay) ? $yesterdayEventNextDayStay : 0;
                            $list[$i]['yesterday_event_next_day_stay_cost'] = ! empty($yesterdayEventNextDayStayCost) ? $yesterdayEventNextDayStayCost : 0;
                            $list[$i]['yesterday_event_next_day_stay_ratio'] = ! empty($yesterdayEventNextDayStayRatio) ? $yesterdayEventNextDayStayRatio : 0;
                            $list[$i]['event_jin_jian_app'] = array_sum(array_column($tav, 'event_jin_jian_app'));
                            $list[$i]['event_jin_jian_app_cost'] = ! empty($list[$i]['event_jin_jian_app']) ? round($list[$i]['charge'] / $list[$i]['event_jin_jian_app'], 3) : 0;
                            $list[$i]['form_count'] = array_sum(array_column($tav, 'form_count'));
                            $list[$i]['form_cost'] = ! empty($list[$i]['form_count']) ? round($list[$i]['charge'] / $list[$i]['form_count'], 3) : 0;
                            $list[$i]['form_action_ratio'] = ! empty($list[$i]['activation']) ? round($list[$i]['form_count'] / $list[$i]['activation'] * 100, 2) . '%' : 0;
                            $list[$i]['event_new_user_pay'] = array_sum(array_column($tav, 'event_new_user_pay'));
                            $list[$i]['event_new_user_pay_cost'] = ! empty($list[$i]['event_new_user_pay']) ? round($list[$i]['charge'] / $list[$i]['event_new_user_pay'], 3) : 0;
                            $h = $list[$i]['activation'] + $list[$i]['form_count'];
                            $list[$i]['event_new_user_pay_ratio'] = ! empty($h) ? round($list[$i]['event_new_user_pay'] / $h * 100, 2) . '%' : 0;
                            $list[$i]['event_valid_clues'] = array_sum(array_column($tav, 'event_valid_clues'));
                            $list[$i]['event_valid_clues_cost'] = ! empty($list[$i]['event_valid_clues']) ? round($list[$i]['charge'] / $list[$i]['event_valid_clues'], 3) : 0;
                            $list[$i]['event_new_user_jinjian_app'] = array_sum(array_column($tav, 'event_new_user_jinjian_app'));
                            $list[$i]['event_new_user_jinjian_app_cost'] = ! empty($list[$i]['event_new_user_jinjian_app']) ? round($list[$i]['charge'] / $list[$i]['event_new_user_jinjian_app'], 3) : 0;
                            $h = $list[$i]['activation'] + $list[$i]['form_count'];
                            $list[$i]['event_new_user_jinjian_app_roi'] = ! empty($h) ? round($list[$i]['event_new_user_jinjian_app'] / $h * 100, 2) . '%' : 0;
                            $list[$i]['event_credit_grant_app'] = array_sum(array_column($tav, 'event_credit_grant_app'));
                            $list[$i]['event_credit_grant_app_cost'] = ! empty($list[$i]['event_credit_grant_app']) ? round($list[$i]['charge'] / $list[$i]['event_credit_grant_app'], 3) : 0;
                            $list[$i]['event_credit_grant_app_ratio'] = ! empty($list[$i]['event_jin_jian_app']) ? round($list[$i]['event_credit_grant_app'] / $list[$i]['event_jin_jian_app'] * 100, 2) . '%' : 0;
                            $list[$i]['event_new_user_credit_grant_app'] = array_sum(array_column($tav, 'event_new_user_credit_grant_app'));
                            $list[$i]['event_new_user_credit_grant_app_cost'] = ! empty($list[$i]['event_new_user_credit_grant_app']) ? round($list[$i]['charge'] / $list[$i]['event_new_user_credit_grant_app'], 3) : 0;
                            $h = $list[$i]['activation'] + $list[$i]['form_count'];
                            $list[$i]['event_new_user_credit_grant_app_roi'] = ! empty($h) ? round($list[$i]['event_new_user_credit_grant_app'] / $h * 100, 2) . '%' : 0;
                            $list[$i]['event_credit_grant_first_day_app'] = array_sum(array_column($tav, 'event_credit_grant_first_day_app'));
                            $list[$i]['event_order_submit'] = array_sum(array_column($tav, 'event_order_submit'));
                            $list[$i]['event_order_paid_roi'] = 0;
                            $list[$i]['event_add_shopping_cart'] = array_sum(array_column($tav, 'event_add_shopping_cart'));
                            $list[$i]['event_add_shopping_cart_cost'] = ! empty($list[$i]['event_add_shopping_cart']) ? round($list[$i]['charge'] / $list[$i]['event_add_shopping_cart'], 3) : 0;
                            $list[$i]['event_watch_app_ad'] = array_sum(array_column($tav, 'event_watch_app_ad'));
                            $list[$i]['event_ad_watch_times'] = array_sum(array_column($tav, 'event_ad_watch_times'));
                            $list[$i]['event_ad_watch_times_cost'] = ! empty($list[$i]['event_ad_watch_times']) ? round($list[$i]['charge'] / $list[$i]['event_ad_watch_times'], 3) : 0;
                            $list[$i]['event_ad_watch_times_ratio'] = ! empty($list[$i]['activation']) ? round($list[$i]['event_ad_watch_times'] / $list[$i]['activation'] * 100, 2) . '%' : 0;

                            $list[$i]['event_app_invoked'] = array_sum(array_column($tav, 'event_app_invoked')); // 唤醒应用数
                            $list[$i]['event_app_invoked_cost'] = array_sum(array_column($tav, 'event_app_invoked_cost')); // 唤醒应用成本
                            $list[$i]['event_app_invoked_ratio'] = ! empty($list[$i]['bclick']) ? round($list[$i]['event_app_invoked'] / $list[$i]['bclick'] * 100, 2) . '%' : 0; // 唤醒应用率 event_app_invoked / bclick

                            $list[$i]['conversion_num_by_impression_7d'] = array_sum(array_column($tav, 'conversion_num_by_impression_7d')); // 转化数(计费时间)

                            $list[$i]['stat_date'] = date('Y-m-d', intval(explode('_', $tak)[0]));
                            $list[$i]['stat_hour'] = intval(date('H', intval(explode('_', $tak)[0])));
                            if ($dimension == 3) {
                                $list[$i]['campaign_id'] = explode('_', $tak)[1];
                                $list[$i]['campaign_name'] = $tav[0]['campaign_name'];
                            } elseif ($dimension == 2) {
                                $list[$i]['unit_id'] = explode('_', $tak)[1];
                                $list[$i]['unit_name'] = $tav[0]['unit_name'];
                                $list[$i]['campaign_id'] = $tav[0]['campaign_id'];
                                $list[$i]['campaign_name'] = $tav[0]['campaign_name'];
                            }
                            ++$i;
                        }
                    }
                }
                // 拼接基础数据
                if (! empty($list)) {
                    // 优化师
                    $operation = (new ClientAccountOperationTimeModel())->alias('a')->leftJoin(['habo_staff' => 'b'], 'a.staff_id=b.id')->where('a.advertiser_id', $advertiserId)->whereNull('a.end_date')->value('staff_name');
                    foreach ($list as $key => $item) {
                        // 获取基础数据、昨日消耗和环比=
                        // 优化师
                        $list[$key]['operation_name'] = $operation;
                        // 广告主信息
                        $list[$key]['advertiser_id'] = $advertiserId;
                        $list[$key]['account_name'] = ! empty($advInfos[$advertiserId]) ? $advInfos[$advertiserId] : '';
                        // 日期、时间
                        $list[$key]['date'] = $report_date_type == 1 ? date('Y-m-d', strtotime(strval($sendDate))) : date('Y-m-d', $sendYesterdayDate);
                        $list[$key]['time'] = $sendTime . ':00';
                        if ($is_retain == 1) {
                            $list[$key]['time'] = $item['stat_hour'] . ':00';
                        }
                        // 产品
                        $list[$key]['product_name'] = ! empty($productInfos[$advertiserId]) ? $productInfos[$advertiserId] : '';
                        // 代理商
                        $list[$key]['agent'] = '上海艾秒广告有限公司';
                        // 媒体
                        $list[$key]['media'] = '快手-磁力引擎';
                        $list[$key]['data_sort'] = $advertiserKey;
                        if ($dimension == 1) {
                            // 昨日同时段消耗、昨日同时段消耗环比
                            if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_ratio', $add_fields)) {
                                // 昨日同时段消耗
                                $list[$key]['yesterday_cost'] = self::getAccountCost($advertiserId, intval(date('Ymd', $sendYesterdayDate)), intval($item['stat_hour']), $report_date_type, 'charge');
                                // 昨日同时段消耗环比
                                $x = bcsub(strval($item['charge']), strval($list[$key]['yesterday_cost']), 3);
                                $list[$key]['yesterday_cost_ratio'] = ! empty($list[$key]['yesterday_cost']) ? round($x / $list[$key]['yesterday_cost'] * 100, 2) . '%' : 0;
                            }
                            // 付费成本环比
                            if (in_array('event_new_user_pay_ringgit', $add_fields)) {
                                // 昨日同时段新增付费人数成本
                                $list[$key]['event_new_user_pay_cost_yesterday'] = self::getAccountCost($advertiserId, intval(date('Ymd', $sendYesterdayDate)), intval($item['stat_hour']), $report_date_type, 'event_new_user_pay_cost');
                                $list[$key]['event_new_user_pay_cost_yesterday'] = ! empty($list[$key]['event_new_user_pay_cost_yesterday']) ? $list[$key]['event_new_user_pay_cost_yesterday'] : 0;
                                // 付费成本环比
                                $list[$key]['event_new_user_pay_ringgit'] = ! empty($list[$key]['event_new_user_pay_cost_yesterday']) ? round($list[$key]['event_new_user_pay_cost'] / $list[$key]['event_new_user_pay_cost_yesterday'] - 1, 2) : 0;
                            }
                            if ($report_date_type == 2) {
                                // 次日次留
                                if (in_array('event_next_day_stay', $add_fields) || in_array('event_next_day_stay_cost', $add_fields) || in_array('event_next_day_stay_ratio', $add_fields)) {
                                    $list[$key]['event_next_day_stay'] = ! empty($eventNextDayStay) ? $eventNextDayStay : 0;
                                    $list[$key]['event_next_day_stay_cost'] = ! empty($eventNextDayStayCost) ? $eventNextDayStayCost : 0;
                                    $list[$key]['event_next_day_stay_ratio'] = ! empty($eventNextDayStayRatio) ? $eventNextDayStayRatio : 0;
                                }
                                // 昨日次留
                                if (in_array('yesterday_event_next_day_stay', $add_fields) || in_array('yesterday_event_next_day_stay_cost', $add_fields) || in_array('yesterday_event_next_day_stay_ratio', $add_fields)) {
                                    $list[$key]['yesterday_event_next_day_stay'] = ! empty($yesterdayEventNextDayStay) ? $yesterdayEventNextDayStay : 0;
                                    $list[$key]['yesterday_event_next_day_stay_cost'] = ! empty($yesterdayEventNextDayStayCost) ? $yesterdayEventNextDayStayCost : 0;
                                    $list[$key]['yesterday_event_next_day_stay_ratio'] = ! empty($yesterdayEventNextDayStayRatio) ? $yesterdayEventNextDayStayRatio : 0;
                                }
                            }
                        } elseif ($dimension == 3) {
                            // 日预算、创建时间
                            //  $info = KsCampaignDspModel::where('advertiser_id', $advertiserId)->where('campaign_id', $item['campaign_id'])->field('day_budget,create_time')->find();
                            //  $list[$key]['day_budget'] = !empty($info['day_budget'])?$info['day_budget']:'0';
                            //  $list[$key]['create_time'] = !empty($info['create_time'])?$info['create_time']:'';
                            $list[$key]['day_budget'] = '0';
                            $list[$key]['create_time'] = '';
                            // 昨日同时段消耗、昨日同时段消耗环比
                            if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_ratio', $add_fields)) {
                                // 昨日同时段消耗
                                $list[$key]['yesterday_cost'] = self::getCampaignCost($advertiserId, $val['campaign_id'], intval(date('Ymd', $sendYesterdayDate)), intval($item['stat_hour']), $report_date_type, 'charge');
                                // 昨日同时段消耗环比
                                $x = bcsub(strval($item['charge']), strval($list[$key]['yesterday_cost']), 3);
                                $list[$key]['yesterday_cost_ratio'] = ! empty($list[$key]['yesterday_cost']) ? round($x / $list[$key]['yesterday_cost'] * 100, 2) . '%' : 0;
                            }
                            // 付费成本环比
                            if (in_array('event_new_user_pay_ringgit', $add_fields)) {
                                // 昨日同时段新增付费人数成本
                                $list[$key]['event_new_user_pay_cost_yesterday'] = self::getCampaignCost($advertiserId, $val['campaign_id'], intval(date('Ymd', $sendYesterdayDate)), intval($item['stat_hour']), $report_date_type, 'event_new_user_pay_cost');
                                $list[$key]['event_new_user_pay_cost_yesterday'] = ! empty($list[$key]['event_new_user_pay_cost_yesterday']) ? $list[$key]['event_new_user_pay_cost_yesterday'] : 0;
                                // 付费成本环比
                                $list[$key]['event_new_user_pay_ringgit'] = ! empty($list[$key]['event_new_user_pay_cost_yesterday']) ? round($list[$key]['event_new_user_pay_cost'] / $list[$key]['event_new_user_pay_cost_yesterday'] - 1, 2) : 0;
                            }
                        } elseif ($dimension == 2) {
                            // 出价、ROI系数、日预算、创建时间、优化目标、深度转化目标
                            $info = (new ReportRecordPushKsUnitModel())->where('advertiser_id', $advertiserId)->where('unit_id', $item['unit_id'])->find()->toArray();
                            $list[$key]['bid'] = 0;
                            $list[$key]['roi_ratio'] = 0;
                            $list[$key]['day_budget'] = 0;
                            $list[$key]['create_time'] = '';
                            $list[$key]['ocpx_action_type'] = 0;
                            $list[$key]['deep_conversion_type'] = 0;
                            if (! empty($data)) {
                                $dataContent = json_decode($info['data_content'], true);
                                $list[$key]['bid'] = ! empty($info['bid']) ? $info['bid'] : 0;
                                $list[$key]['roi_ratio'] = ! empty($dataContent['roi_ratio']) ? $dataContent['roi_ratio'] : 0;
                                $list[$key]['day_budget'] = ! empty($dataContent['day_budget']) ? $dataContent['day_budget'] : 0;
                                $list[$key]['create_time'] = ! empty($dataContent['create_time']) ? $dataContent['create_time'] : '';
                                $list[$key]['ocpx_action_type'] = ! empty($dataContent['ocpx_action_type']) ? $dataContent['ocpx_action_type'] : 0;
                                $list[$key]['deep_conversion_type'] = ! empty($dataContent['deep_conversion_type']) ? $dataContent['deep_conversion_type'] : 0;
                                $list[$key]['campaign_id'] = ! empty($item['campaign_id']) ? $item['campaign_id'] : '';
                                $list[$key]['campaign_name'] = ! empty($item['campaign_name']) ? $item['campaign_name'] : '';
                            }
                            // 昨日同时段消耗、昨日同时段消耗环比
                            if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_ratio', $add_fields)) {
                                // 昨日同时段消耗
                                $list[$key]['yesterday_cost'] = self::getUnitCost($advertiserId, $val['unit_id'], intval(date('Ymd', $sendYesterdayDate)), intval($item['stat_hour']), $report_date_type, 'charge');
                                // 昨日同时段消耗环比
                                $x = bcsub(strval($item['charge']), strval($list[$key]['yesterday_cost']), 3);
                                $list[$key]['yesterday_cost_ratio'] = ! empty($list[$key]['yesterday_cost']) ? round($x / $list[$key]['yesterday_cost'] * 100, 2) . '%' : 0;
                            }
                            // 付费成本环比
                            if (in_array('event_new_user_pay_ringgit', $add_fields)) {
                                // 昨日同时段新增付费人数成本
                                $list[$key]['event_new_user_pay_cost_yesterday'] = self::getUnitCost($advertiserId, $val['unit_id'], intval(date('Ymd', $sendYesterdayDate)), intval($item['stat_hour']), $report_date_type, 'event_new_user_pay_cost');
                                $list[$key]['event_new_user_pay_cost_yesterday'] = ! empty($list[$key]['event_new_user_pay_cost_yesterday']) ? $list[$key]['event_new_user_pay_cost_yesterday'] : 0;
                                // 付费成本环比
                                $list[$key]['event_new_user_pay_ringgit'] = ! empty($list[$key]['event_new_user_pay_cost_yesterday']) ? round($list[$key]['event_new_user_pay_cost'] / $list[$key]['event_new_user_pay_cost_yesterday'] - 1, 2) : 0;
                            }
                        }
                        // 平均千次封面曝光花费
                        if (in_array('show_1k_cost', $add_fields)) {
                            $list[$key]['show_1k_cost'] = ! empty($item['show']) ? round($item['charge'] / $item['show'] * 1000, 3) : 0;
                        }
                        // 平均千次素材曝光花费
                        if (in_array('aclick_1k_cost', $add_fields)) {
                            $list[$key]['aclick_1k_cost'] = ! empty($item['aclick']) ? round($item['charge'] / $item['aclick'] * 1000, 3) : 0;
                        }
                        // 折后消耗
                        if (in_array('cash_cost', $add_fields)) {
                            $list[$key]['cash_cost'] = ! empty($cash_cost_ratio) ? round($item['charge'] / $cash_cost_ratio, 2) : 0;
                        }
                        // 折后成本
                        if (in_array('folding_convert', $add_fields)) {
                            $list[$key]['folding_convert'] = ! empty($folding_convert_ratio) ? round($item[$folding_convert_field] / $folding_convert_ratio, 2) : 0;
                        }
                        // 付费次数成本
                        if (in_array('event_pay_cost', $add_fields)) {
                            $list[$key]['event_pay_cost'] = ! empty($item['event_pay']) ? round($item['charge'] / $item['event_pay'], 3) : 0;
                        }
                        // 素材点击率
                        if (in_array('aclick_ratio', $add_fields) || in_array('aclick', $add_fields) || in_array('bclick', $add_fields)) {
                            $list[$key]['aclick_ratio'] = ! empty($item['aclick']) ? round($item['bclick'] / $item['aclick'] * 100, 2) . '%' : 0;
                        }
                        // 自定义数据
                        if (! empty($custom_fields_name)) {
                            foreach ($custom_fields_name as $cfnv) {
                                $molecule = ! empty($list[$key][$cfnv['molecule']]) ? str_replace('%', '', $list[$key][$cfnv['molecule']]) : 0;
                                $denominator = ! empty($list[$key][$cfnv['denominator']]) ? str_replace('%', '', $list[$key][$cfnv['denominator']]) : 0;
                                $list[$key][$cfnv['field']] = $denominator > 0 ? round($molecule / $denominator, 2) : 0;
                            }
                        }
                    }
                }
                if (! empty($list) && $dimension == 1) {
                    foreach ($list as $i => $val) {
                        $list[$i]['charge'] = ! empty($val['charge']) ? $val['charge'] : 0;
                        $list[$i]['show'] = ! empty($val['show']) ? $val['show'] : 0;
                        $list[$i]['photo_click'] = ! empty($val['photo_click']) ? $val['photo_click'] : 0;
                        $list[$i]['aclick'] = ! empty($val['aclick']) ? $val['aclick'] : 0;
                        $list[$i]['bclick'] = ! empty($val['bclick']) ? $val['bclick'] : 0;
                        $list[$i]['photo_click_ratio'] = ! empty($val['photo_click_ratio']) ? $val['photo_click_ratio'] : 0;
                        $list[$i]['aclick_ratio'] = ! empty($val['aclick_ratio']) ? $val['aclick_ratio'] : 0;
                        $list[$i]['cash_cost'] = ! empty($val['cash_cost']) ? $val['cash_cost'] : 0;
                        $list[$i]['yesterday_cost_ratio'] = ! empty($val['yesterday_cost_ratio']) ? $val['yesterday_cost_ratio'] : 0;
                        $list[$i]['yesterday_cost'] = ! empty($val['yesterday_cost']) ? $val['yesterday_cost'] : 0;
                        $list[$i]['show_1k_cost'] = ! empty($val['show_1k_cost']) ? $val['show_1k_cost'] : 0;
                        $list[$i]['aclick_1k_cost'] = ! empty($val['aclick_1k_cost']) ? $val['aclick_1k_cost'] : 0;
                        $list[$i]['photo_click_cost'] = ! empty($val['photo_click_cost']) ? $val['photo_click_cost'] : 0;
                        $list[$i]['action_cost'] = ! empty($val['action_cost']) ? round($val['action_cost'], 3) : 0;
                        $list[$i]['download_started'] = ! empty($val['download_started']) ? $val['download_started'] : 0;
                        $list[$i]['download_completed'] = ! empty($val['download_completed']) ? $val['download_completed'] : 0;
                        $list[$i]['download_completed_cost'] = ! empty($val['download_completed_cost']) ? $val['download_completed_cost'] : 0;
                        $list[$i]['download_completed_ratio'] = ! empty($val['download_completed_ratio']) ? $val['download_completed_ratio'] : 0;
                        $list[$i]['download_started_cost'] = ! empty($val['download_started_cost']) ? $val['download_started_cost'] : 0;
                        $list[$i]['download_started_ratio'] = ! empty($val['download_started_ratio']) ? $val['download_started_ratio'] : 0;
                        $list[$i]['key_action'] = ! empty($val['key_action']) ? $val['key_action'] : 0;
                        $list[$i]['key_action_cost'] = ! empty($val['key_action_cost']) ? $val['key_action_cost'] : 0;
                        $list[$i]['key_action_ratio'] = ! empty($val['key_action_ratio']) ? $val['key_action_ratio'] : 0;
                        $list[$i]['activation'] = ! empty($val['activation']) ? $val['activation'] : 0;
                        $list[$i]['conversion_cost'] = ! empty($val['conversion_cost']) ? round($val['conversion_cost'], 3) : 0;
                        $list[$i]['download_conversion_ratio'] = ! empty($val['download_conversion_ratio']) ? $val['download_conversion_ratio'] : 0;
                        $list[$i]['event_new_user_pay'] = ! empty($val['event_new_user_pay']) ? $val['event_new_user_pay'] : 0;
                        $list[$i]['event_new_user_pay_cost'] = ! empty($val['event_new_user_pay_cost']) ? $val['event_new_user_pay_cost'] : 0;
                        $list[$i]['event_new_user_pay_ratio'] = ! empty($val['event_new_user_pay_ratio']) ? $val['event_new_user_pay_ratio'] : 0;
                        $list[$i]['event_register'] = ! empty($val['event_register']) ? $val['event_register'] : 0;
                        $list[$i]['event_register_cost'] = ! empty($val['event_register_cost']) ? $val['event_register_cost'] : 0;
                        $list[$i]['event_register_ratio'] = ! empty($val['event_register_ratio']) ? $val['event_register_ratio'] : 0;
                        $list[$i]['event_pay_first_day'] = ! empty($val['event_pay_first_day']) ? $val['event_pay_first_day'] : 0;
                        $list[$i]['event_pay_purchase_amount_first_day'] = ! empty($val['event_pay_purchase_amount_first_day']) ? $val['event_pay_purchase_amount_first_day'] : 0;
                        $list[$i]['event_pay_first_day_cost'] = ! empty($val['event_pay_first_day_cost']) ? $val['event_pay_first_day_cost'] : 0;
                        $list[$i]['event_pay_first_day_roi'] = ! empty($val['event_pay_first_day_roi']) ? $val['event_pay_first_day_roi'] : 0;
                        $list[$i]['event_pay'] = ! empty($val['event_pay']) ? $val['event_pay'] : 0;
                        $list[$i]['event_pay_purchase_amount'] = ! empty($val['event_pay_purchase_amount']) ? $val['event_pay_purchase_amount'] : 0;
                        $list[$i]['event_pay_cost'] = ! empty($val['event_pay_cost']) ? $val['event_pay_cost'] : 0;
                        $list[$i]['event_pay_roi'] = ! empty($val['event_pay_roi']) ? $val['event_pay_roi'] : 0;
                        // $list[$i]['event_next_day_stay'] = !empty($val['event_next_day_stay'])?$val['event_next_day_stay']:0;
                        // $list[$i]['event_next_day_stay_cost'] = !empty($val['event_next_day_stay_cost'])?$val['event_next_day_stay_cost']:0;
                        // $list[$i]['event_next_day_stay_ratio'] = !empty($val['event_next_day_stay_ratio'])?$val['event_next_day_stay_ratio']:0;
                        $list[$i]['event_next_day_stay'] = ! empty($eventNextDayStay) ? $eventNextDayStay : 0;
                        $list[$i]['event_next_day_stay_cost'] = ! empty($eventNextDayStayCost) ? $eventNextDayStayCost : 0;
                        $list[$i]['event_next_day_stay_ratio'] = ! empty($eventNextDayStayRatio) ? $eventNextDayStayRatio : 0;
                        $list[$i]['yesterday_event_next_day_stay'] = $yesterdayEventNextDayStay ?? 0;
                        $list[$i]['yesterday_event_next_day_stay_cost'] = $yesterdayEventNextDayStayCost ?? 0;
                        $list[$i]['yesterday_event_next_day_stay_ratio'] = $yesterdayEventNextDayStayRatio ?? 0;
                        $list[$i]['event_jin_jian_app'] = ! empty($val['event_jin_jian_app']) ? $val['event_jin_jian_app'] : 0;
                        $list[$i]['event_jin_jian_app_cost'] = ! empty($val['event_jin_jian_app_cost']) ? $val['event_jin_jian_app_cost'] : 0;
                        $list[$i]['form_count'] = ! empty($val['form_count']) ? $val['form_count'] : 0;
                        $list[$i]['event_wechat_connected'] = ! empty($val['event_wechat_connected']) ? $val['event_wechat_connected'] : 0;
                        $list[$i]['form_cost'] = ! empty($val['form_cost']) ? $val['form_cost'] : 0;
                        $list[$i]['form_action_ratio'] = ! empty($val['form_action_ratio']) ? $val['form_action_ratio'] : 0;
                        $list[$i]['event_valid_clues'] = ! empty($val['event_valid_clues']) ? $val['event_valid_clues'] : 0;
                        $list[$i]['event_valid_clues_cost'] = ! empty($val['event_valid_clues_cost']) ? $val['event_valid_clues_cost'] : 0;
                        $list[$i]['event_new_user_jinjian_app'] = ! empty($val['event_new_user_jinjian_app']) ? $val['event_new_user_jinjian_app'] : 0;
                        $list[$i]['event_new_user_jinjian_app_cost'] = ! empty($val['event_new_user_jinjian_app_cost']) ? $val['event_new_user_jinjian_app_cost'] : 0;
                        $list[$i]['event_new_user_jinjian_app_roi'] = ! empty($val['event_new_user_jinjian_app_roi']) ? $val['event_new_user_jinjian_app_roi'] : 0;
                        $list[$i]['event_new_user_credit_grant_app'] = ! empty($val['event_new_user_credit_grant_app']) ? $val['event_new_user_credit_grant_app'] : 0;
                        $list[$i]['event_new_user_credit_grant_app_cost'] = ! empty($val['event_new_user_credit_grant_app_cost']) ? $val['event_new_user_credit_grant_app_cost'] : 0;
                        $list[$i]['event_new_user_credit_grant_app_roi'] = ! empty($val['event_new_user_credit_grant_app_roi']) ? $val['event_new_user_credit_grant_app_roi'] : 0;
                        $list[$i]['event_credit_grant_first_day_app'] = ! empty($val['event_credit_grant_first_day_app']) ? $val['event_credit_grant_first_day_app'] : 0;
                        $list[$i]['event_order_paid_roi'] = ! empty($val['event_order_paid_roi']) ? $val['event_order_paid_roi'] : 0;
                        $list[$i]['event_order_submit'] = ! empty($val['event_order_submit']) ? $val['event_order_submit'] : 0;
                        $list[$i]['event_add_shopping_cart'] = ! empty($val['event_add_shopping_cart']) ? $val['event_add_shopping_cart'] : 0;
                        $list[$i]['event_add_shopping_cart_cost'] = ! empty($val['event_add_shopping_cart_cost']) ? $val['event_add_shopping_cart_cost'] : 0;
                        $list[$i]['event_watch_app_ad'] = ! empty($val['event_watch_app_ad']) ? $val['event_watch_app_ad'] : 0;
                        $list[$i]['event_ad_watch_times'] = ! empty($val['event_ad_watch_times']) ? $val['event_ad_watch_times'] : 0;
                        $list[$i]['event_ad_watch_times_ratio'] = ! empty($val['event_ad_watch_times_ratio']) ? $val['event_ad_watch_times_ratio'] : 0;
                        $list[$i]['event_ad_watch_times_cost'] = ! empty($val['event_ad_watch_times_cost']) ? $val['event_ad_watch_times_cost'] : 0;
                        $list[$i]['event_credit_grant_app'] = ! empty($val['event_credit_grant_app']) ? $val['event_credit_grant_app'] : 0;
                        $list[$i]['event_credit_grant_app_cost'] = ! empty($val['event_credit_grant_app_cost']) ? $val['event_credit_grant_app_cost'] : 0;
                        $list[$i]['event_credit_grant_app_ratio'] = ! empty($val['event_credit_grant_app_ratio']) ? $val['event_credit_grant_app_ratio'] : 0;

                        $list[$i]['event_app_invoked'] = ! empty($val['event_app_invoked']) ? $val['event_app_invoked'] : 0; // 唤醒应用数
                        $list[$i]['event_app_invoked_cost'] = ! empty($val['event_app_invoked_cost']) ? $val['event_app_invoked_cost'] : 0; // 唤醒应用成本
                        $list[$i]['event_app_invoked_ratio'] = ! empty($val['event_app_invoked_ratio']) ? $val['event_app_invoked_ratio'] : 0; // 唤醒应用率 event_app_invoked / bclick

                        $list[$i]['conversion_num_by_impression_7d'] = ! empty($val['conversion_num_by_impression_7d']) ? $val['conversion_num_by_impression_7d'] : 0; // 转化数(计费时间)

                        // 获取基础数据、昨日消耗和环比
                        // 优化师
                        $list[$i]['operation_name'] = (new ClientAccountOperationTimeModel())->alias('a')->leftJoin(['habo_staff' => 'b'], 'a.staff_id=b.id')->where('a.advertiser_id', $advertiserId)->whereNull('a.end_date')->value('staff_name');
                        // 广告主信息
                        $list[$i]['advertiser_id'] = $advertiserId;
                        $list[$i]['account_name'] = ! empty($advInfos[$advertiserId]) ? $advInfos[$advertiserId] : '';
                        // 日期、时间
                        $list[$i]['date'] = $report_date_type == 1 ? date('Y-m-d', strtotime(strval($sendDate))) : date('Y-m-d', $sendYesterdayDate);
                        $list[$i]['time'] = $sendTime . ':00';
                        $list[$i]['stat_hour'] = intval(date('H', strtotime($sendDate . ' ' . $sendTime . ':00')));
                        // 产品
                        $list[$i]['product_name'] = ! empty($productInfos[$advertiserId]) ? $productInfos[$advertiserId] : '';
                        // 代理商
                        $list[$i]['agent'] = '上海艾秒广告有限公司';
                        // 媒体
                        $list[$i]['media'] = '快手-磁力引擎';
                        $list[$i]['data_sort'] = $advertiserKey;
                        // 昨日同时段消耗、昨日同时段消耗环比
                        if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_rate', $add_fields)) {
                            // 昨日同时段消耗
                            $list[$i]['yesterday_cost'] = self::getAccountCost($advertiserId, intval(date('Ymd', $sendYesterdayDate)), intval($val['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'charge');
                            $list[$i]['yesterday_cost'] = ! empty($val['yesterday_cost']) ? $val['yesterday_cost'] : 0;
                            // 昨日同时段消耗环比
                            $x = bcsub(strval($val['charge']), strval($list[$i]['yesterday_cost']), 3);
                            $list[$i]['yesterday_cost_rate'] = ! empty($list[$i]['yesterday_cost']) ? round($x / $list[$i]['yesterday_cost'] * 100, 2) . '%' : 0;
                        }
                        // 付费成本环比
                        if (in_array('event_new_user_pay_ringgit', $add_fields)) {
                            // 昨日同时段新增付费人数成本
                            $list[$i]['event_new_user_pay_cost_yesterday'] = self::getAccountCost($advertiserId, intval(date('Ymd', $sendYesterdayDate)), intval($val['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'event_new_user_pay_cost');
                            $list[$i]['event_new_user_pay_cost_yesterday'] = ! empty($list[$i]['event_new_user_pay_cost_yesterday']) ? $list[$i]['event_new_user_pay_cost_yesterday'] : 0;
                            // 付费成本环比
                            $list[$i]['event_new_user_pay_ringgit'] = ! empty($list[$i]['event_new_user_pay_cost_yesterday']) ? round($list[$i]['event_new_user_pay_cost'] / $list[$i]['event_new_user_pay_cost_yesterday'] - 1, 2) : 0;
                        }
                        // 折后消耗
                        if (in_array('cash_cost', $add_fields)) {
                            $list[$i]['cash_cost'] = ! empty($cash_cost_ratio) ? round($val['charge'] / $cash_cost_ratio, 2) : 0;
                        }
                        // 折后成本
                        if (in_array('folding_convert', $add_fields)) {
                            $list[$i]['folding_convert'] = ! empty($folding_convert_ratio) ? round($list[$i][$folding_convert_field] / $folding_convert_ratio, 2) : 0;
                        }
                        // 自定义数据
                        if (! empty($custom_fields_name)) {
                            foreach ($custom_fields_name as $cfnv) {
                                $molecule = ! empty($list[$i][$cfnv['molecule']]) ? str_replace('%', '', $list[$i][$cfnv['molecule']]) : 0;
                                $denominator = ! empty($list[$i][$cfnv['denominator']]) ? str_replace('%', '', $list[$i][$cfnv['denominator']]) : 0;
                                $list[$i][$cfnv['field']] = $denominator > 0 ? round($molecule / $denominator, 2) : 0;
                            }
                        }
                    }
                }

                $emptySendTimes = array_diff($send_time, array_column($list, 'time'));
                // 数据补零
                if ($emptySendTimes) {
                    foreach ($emptySendTimes as $sv) {
                        if (strtotime($sendDate . ' ' . $sv . ':00') > strtotime($sendDate . ' ' . $sendTime . ':00') || ! ($report_date_type == 1 && $is_retain == 1)) {
                            continue;
                        }
                        if ($dimension == 1) {
                            $i = count($list);
                            $list[$i]['charge'] = ! empty($list[$i]['charge']) ? $list[$i]['charge'] : 0;
                            $list[$i]['show'] = ! empty($list[$i]['show']) ? $list[$i]['show'] : 0;
                            $list[$i]['photo_click'] = ! empty($list[$i]['photo_click']) ? $list[$i]['photo_click'] : 0;
                            $list[$i]['aclick'] = ! empty($list[$i]['aclick']) ? $list[$i]['aclick'] : 0;
                            $list[$i]['bclick'] = ! empty($list[$i]['bclick']) ? $list[$i]['bclick'] : 0;
                            $list[$i]['photo_click_ratio'] = ! empty($list[$i]['photo_click_ratio']) ? $list[$i]['photo_click_ratio'] : 0;
                            $list[$i]['aclick_ratio'] = ! empty($list[$i]['aclick_ratio']) ? $list[$i]['aclick_ratio'] : 0;
                            $list[$i]['cash_cost'] = ! empty($list[$i]['cash_cost']) ? $list[$i]['cash_cost'] : 0;
                            $list[$i]['yesterday_cost_ratio'] = ! empty($list[$i]['yesterday_cost_ratio']) ? $list[$i]['yesterday_cost_ratio'] : 0;
                            $list[$i]['yesterday_cost'] = ! empty($list[$i]['yesterday_cost']) ? $list[$i]['yesterday_cost'] : 0;
                            $list[$i]['show_1k_cost'] = ! empty($list[$i]['show_1k_cost']) ? $list[$i]['show_1k_cost'] : 0;
                            $list[$i]['aclick_1k_cost'] = ! empty($list[$i]['aclick_1k_cost']) ? $list[$i]['aclick_1k_cost'] : 0;
                            $list[$i]['photo_click_cost'] = ! empty($list[$i]['photo_click_cost']) ? $list[$i]['photo_click_cost'] : 0;
                            $list[$i]['action_cost'] = ! empty($list[$i]['action_cost']) ? $list[$i]['action_cost'] : 0;
                            $list[$i]['download_started'] = ! empty($list[$i]['download_started']) ? $list[$i]['download_started'] : 0;
                            $list[$i]['download_completed'] = ! empty($list[$i]['download_completed']) ? $list[$i]['download_completed'] : 0;
                            $list[$i]['download_completed_cost'] = ! empty($list[$i]['download_completed_cost']) ? $list[$i]['download_completed_cost'] : 0;
                            $list[$i]['download_completed_ratio'] = ! empty($list[$i]['download_completed_ratio']) ? $list[$i]['download_completed_ratio'] : 0;
                            $list[$i]['download_started_cost'] = ! empty($list[$i]['download_started_cost']) ? $list[$i]['download_started_cost'] : 0;
                            $list[$i]['download_started_ratio'] = ! empty($list[$i]['download_started_ratio']) ? $list[$i]['download_started_ratio'] : 0;
                            $list[$i]['key_action'] = ! empty($list[$i]['key_action']) ? $list[$i]['key_action'] : 0;
                            $list[$i]['key_action_cost'] = ! empty($list[$i]['key_action_cost']) ? $list[$i]['key_action_cost'] : 0;
                            $list[$i]['key_action_ratio'] = ! empty($list[$i]['key_action_ratio']) ? $list[$i]['key_action_ratio'] : 0;
                            $list[$i]['activation'] = ! empty($list[$i]['activation']) ? $list[$i]['activation'] : 0;
                            $list[$i]['conversion_cost'] = ! empty($list[$i]['conversion_cost']) ? $list[$i]['conversion_cost'] : 0;
                            $list[$i]['download_conversion_ratio'] = ! empty($list[$i]['download_conversion_ratio']) ? $list[$i]['download_conversion_ratio'] : 0;
                            $list[$i]['event_new_user_pay'] = ! empty($list[$i]['event_new_user_pay']) ? $list[$i]['event_new_user_pay'] : 0;
                            $list[$i]['event_new_user_pay_cost'] = ! empty($list[$i]['event_new_user_pay_cost']) ? $list[$i]['event_new_user_pay_cost'] : 0;
                            $list[$i]['event_new_user_pay_ratio'] = ! empty($list[$i]['event_new_user_pay_ratio']) ? $list[$i]['event_new_user_pay_ratio'] : 0;
                            $list[$i]['event_register'] = ! empty($list[$i]['event_register']) ? $list[$i]['event_register'] : 0;
                            $list[$i]['event_register_cost'] = ! empty($list[$i]['event_register_cost']) ? $list[$i]['event_register_cost'] : 0;
                            $list[$i]['event_register_ratio'] = ! empty($list[$i]['event_register_ratio']) ? $list[$i]['event_register_ratio'] : 0;
                            $list[$i]['event_pay_first_day'] = ! empty($list[$i]['event_pay_first_day']) ? $list[$i]['event_pay_first_day'] : 0;
                            $list[$i]['event_pay_purchase_amount_first_day'] = ! empty($list[$i]['event_pay_purchase_amount_first_day']) ? $list[$i]['event_pay_purchase_amount_first_day'] : 0;
                            $list[$i]['event_pay_first_day_cost'] = ! empty($list[$i]['event_pay_first_day_cost']) ? $list[$i]['event_pay_first_day_cost'] : 0;
                            $list[$i]['event_pay_first_day_roi'] = ! empty($list[$i]['event_pay_first_day_roi']) ? $list[$i]['event_pay_first_day_roi'] : 0;
                            $list[$i]['event_pay'] = ! empty($list[$i]['event_pay']) ? $list[$i]['event_pay'] : 0;
                            $list[$i]['event_pay_purchase_amount'] = ! empty($list[$i]['event_pay_purchase_amount']) ? $list[$i]['event_pay_purchase_amount'] : 0;
                            $list[$i]['event_pay_cost'] = ! empty($list[$i]['event_pay_cost']) ? $list[$i]['event_pay_cost'] : 0;
                            $list[$i]['event_pay_roi'] = ! empty($list[$i]['event_pay_roi']) ? $list[$i]['event_pay_roi'] : 0;
                            // $list[$i]['event_next_day_stay'] = !empty($list[$i]['event_next_day_stay'])?$list[$i]['event_next_day_stay']:0;
                            // $list[$i]['event_next_day_stay_cost'] = !empty($list[$i]['event_next_day_stay_cost'])?$list[$i]['event_next_day_stay_cost']:0;
                            // $list[$i]['event_next_day_stay_ratio'] = !empty($list[$i]['event_next_day_stay_ratio'])?$list[$i]['event_next_day_stay_ratio']:0;
                            $list[$i]['event_next_day_stay'] = ! empty($eventNextDayStay) ? $eventNextDayStay : 0;
                            $list[$i]['event_next_day_stay_cost'] = ! empty($eventNextDayStayCost) ? $eventNextDayStayCost : 0;
                            $list[$i]['event_next_day_stay_ratio'] = ! empty($eventNextDayStayRatio) ? $eventNextDayStayRatio : 0;
                            $list[$i]['yesterday_event_next_day_stay'] = $yesterdayEventNextDayStay ?? 0;
                            $list[$i]['yesterday_event_next_day_stay_cost'] = $yesterdayEventNextDayStayCost ?? 0;
                            $list[$i]['yesterday_event_next_day_stay_ratio'] = $yesterdayEventNextDayStayRatio ?? 0;
                            $list[$i]['event_jin_jian_app'] = ! empty($list[$i]['event_jin_jian_app']) ? $list[$i]['event_jin_jian_app'] : 0;
                            $list[$i]['event_jin_jian_app_cost'] = ! empty($list[$i]['event_jin_jian_app_cost']) ? $list[$i]['event_jin_jian_app_cost'] : 0;
                            $list[$i]['form_count'] = ! empty($list[$i]['form_count']) ? $list[$i]['form_count'] : 0;
                            $list[$i]['event_wechat_connected'] = ! empty($list[$i]['event_wechat_connected']) ? $list[$i]['event_wechat_connected'] : 0;
                            $list[$i]['form_cost'] = ! empty($list[$i]['form_cost']) ? $list[$i]['form_cost'] : 0;
                            $list[$i]['form_action_ratio'] = ! empty($list[$i]['form_action_ratio']) ? $list[$i]['form_action_ratio'] : 0;
                            $list[$i]['event_valid_clues'] = ! empty($list[$i]['event_valid_clues']) ? $list[$i]['event_valid_clues'] : 0;
                            $list[$i]['event_valid_clues_cost'] = ! empty($list[$i]['event_valid_clues_cost']) ? $list[$i]['event_valid_clues_cost'] : 0;
                            $list[$i]['event_new_user_jinjian_app'] = ! empty($list[$i]['event_new_user_jinjian_app']) ? $list[$i]['event_new_user_jinjian_app'] : 0;
                            $list[$i]['event_new_user_jinjian_app_cost'] = ! empty($list[$i]['event_new_user_jinjian_app_cost']) ? $list[$i]['event_new_user_jinjian_app_cost'] : 0;
                            $list[$i]['event_new_user_jinjian_app_roi'] = ! empty($list[$i]['event_new_user_jinjian_app_roi']) ? $list[$i]['event_new_user_jinjian_app_roi'] : 0;
                            $list[$i]['event_new_user_credit_grant_app'] = ! empty($list[$i]['event_new_user_credit_grant_app']) ? $list[$i]['event_new_user_credit_grant_app'] : 0;
                            $list[$i]['event_new_user_credit_grant_app_cost'] = ! empty($list[$i]['event_new_user_credit_grant_app_cost']) ? $list[$i]['event_new_user_credit_grant_app_cost'] : 0;
                            $list[$i]['event_new_user_credit_grant_app_roi'] = ! empty($list[$i]['event_new_user_credit_grant_app_roi']) ? $list[$i]['event_new_user_credit_grant_app_roi'] : 0;
                            $list[$i]['event_credit_grant_first_day_app'] = ! empty($list[$i]['event_credit_grant_first_day_app']) ? $list[$i]['event_credit_grant_first_day_app'] : 0;
                            $list[$i]['event_order_paid_roi'] = ! empty($list[$i]['event_order_paid_roi']) ? $list[$i]['event_order_paid_roi'] : 0;
                            $list[$i]['event_order_submit'] = ! empty($list[$i]['event_order_submit']) ? $list[$i]['event_order_submit'] : 0;
                            $list[$i]['event_add_shopping_cart'] = ! empty($list[$i]['event_add_shopping_cart']) ? $list[$i]['event_add_shopping_cart'] : 0;
                            $list[$i]['event_add_shopping_cart_cost'] = ! empty($list[$i]['event_add_shopping_cart_cost']) ? $list[$i]['event_add_shopping_cart_cost'] : 0;
                            $list[$i]['event_watch_app_ad'] = ! empty($list[$i]['event_watch_app_ad']) ? $list[$i]['event_watch_app_ad'] : 0;
                            $list[$i]['event_ad_watch_times'] = ! empty($list[$i]['event_ad_watch_times']) ? $list[$i]['event_ad_watch_times'] : 0;
                            $list[$i]['event_ad_watch_times_ratio'] = ! empty($list[$i]['event_ad_watch_times_ratio']) ? $list[$i]['event_ad_watch_times_ratio'] : 0;
                            $list[$i]['event_ad_watch_times_cost'] = ! empty($list[$i]['event_ad_watch_times_cost']) ? $list[$i]['event_ad_watch_times_cost'] : 0;
                            $list[$i]['event_credit_grant_app'] = ! empty($list[$i]['event_credit_grant_app']) ? $list[$i]['event_credit_grant_app'] : 0;
                            $list[$i]['event_credit_grant_app_cost'] = ! empty($list[$i]['event_credit_grant_app_cost']) ? $list[$i]['event_credit_grant_app_cost'] : 0;
                            $list[$i]['event_credit_grant_app_ratio'] = ! empty($list[$i]['event_credit_grant_app_ratio']) ? $list[$i]['event_credit_grant_app_ratio'] : 0;

                            $list[$i]['event_app_invoked'] = ! empty($list[$i]['event_app_invoked']) ? $list[$i]['event_app_invoked'] : 0; // 唤醒应用数
                            $list[$i]['event_app_invoked_cost'] = ! empty($list[$i]['event_app_invoked_cost']) ? $list[$i]['event_app_invoked_cost'] : 0; // 唤醒应用成本
                            $list[$i]['event_app_invoked_ratio'] = ! empty($list[$i]['event_app_invoked_ratio']) ? $list[$i]['event_app_invoked_ratio'] : 0; // 唤醒应用率 event_app_invoked / bclick
                            // 获取基础数据、昨日消耗和环比
                            // 优化师
                            $list[$i]['operation_name'] = (new ClientAccountOperationTimeModel())->alias('a')->leftJoin(['habo_staff' => 'b'], 'a.staff_id=b.id')->where('a.advertiser_id', $advertiserId)->whereNull('a.end_date')->value('staff_name');
                            // 广告主信息
                            $list[$i]['advertiser_id'] = $advertiserId;
                            $list[$i]['account_name'] = ! empty($advInfos[$advertiserId]) ? $advInfos[$advertiserId] : '';
                            // 日期、时间
                            $list[$i]['date'] = date('Y-m-d', strtotime(strval($sendDate)));
                            $list[$i]['time'] = $sv;
                            $list[$i]['stat_hour'] = intval(date('H', strtotime($sendDate . ' ' . $sv . ':00')));
                            // 产品
                            $list[$i]['product_name'] = ! empty($productInfos[$advertiserId]) ? $productInfos[$advertiserId] : '';
                            // 代理商
                            $list[$i]['agent'] = '上海艾秒广告有限公司';
                            // 媒体
                            $list[$i]['media'] = '快手-磁力引擎';
                            $list[$i]['data_sort'] = $advertiserKey;
                            // 昨日同时段消耗、昨日同时段消耗环比
                            if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_rate', $add_fields)) {
                                // 昨日同时段消耗
                                $list[$i]['yesterday_cost'] = self::getAccountCost($advertiserId, intval(date('Ymd', $sendYesterdayDate)), intval($list[$i]['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'charge');
                                $list[$i]['yesterday_cost'] = ! empty($list[$i]['yesterday_cost']) ? $list[$i]['yesterday_cost'] : 0;
                                // 昨日同时段消耗环比
                                $x = bcsub(strval($list[$i]['charge']), strval($list[$i]['yesterday_cost']), 3);
                                $list[$i]['yesterday_cost_rate'] = ! empty($list[$i]['yesterday_cost']) ? round($x / $list[$i]['yesterday_cost'] * 100, 2) . '%' : 0;
                            }
                            // 付费成本环比
                            if (in_array('event_new_user_pay_ringgit', $add_fields)) {
                                // 昨日同时段新增付费人数成本
                                $list[$i]['event_new_user_pay_cost_yesterday'] = self::getAccountCost($advertiserId, intval(date('Ymd', $sendYesterdayDate)), intval($list[$i]['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'event_new_user_pay_cost');
                                $list[$i]['event_new_user_pay_cost_yesterday'] = ! empty($list[$i]['event_new_user_pay_cost_yesterday']) ? $list[$i]['event_new_user_pay_cost_yesterday'] : 0;
                                // 付费成本环比
                                $list[$i]['event_new_user_pay_ringgit'] = ! empty($list[$i]['event_new_user_pay_cost_yesterday']) ? round($list[$i]['event_new_user_pay_cost'] / $list[$i]['event_new_user_pay_cost_yesterday'] - 1, 2) : 0;
                            }
                            // 折后消耗
                            if (in_array('cash_cost', $add_fields)) {
                                $list[$i]['cash_cost'] = ! empty($cash_cost_ratio) ? round($list[$i]['charge'] / $cash_cost_ratio, 2) : 0;
                            }
                            // 折后成本
                            if (in_array('folding_convert', $add_fields)) {
                                $list[$i]['folding_convert'] = ! empty($folding_convert_ratio) ? round($list[$i][$folding_convert_field] / $folding_convert_ratio, 2) : 0;
                            }
                            // 自定义数据
                            if (! empty($custom_fields_name)) {
                                foreach ($custom_fields_name as $cfnv) {
                                    $molecule = ! empty($list[$i][$cfnv['molecule']]) ? str_replace('%', '', $list[$i][$cfnv['molecule']]) : 0;
                                    $denominator = ! empty($list[$i][$cfnv['denominator']]) ? str_replace('%', '', $list[$i][$cfnv['denominator']]) : 0;
                                    $list[$i][$cfnv['field']] = $denominator > 0 ? round($molecule / $denominator, 2) : 0;
                                }
                            }
                        }
                    }
                }
                ++$advertiserKey;
                $dataArr = array_merge($dataArr, $list);
                unset($yesterdayEventNextDayStay, $yesterdayCharge, $yesterdayActivation, $yesterdayEventNextDayStayCost, $yesterdayEventNextDayStayRatio);
            }
            array_multisort(array_column($dataArr, 'stat_hour'), SORT_ASC, array_column($dataArr, 'data_sort'), SORT_ASC, $dataArr);
            if ($dataSort == 2 && in_array('charge', $add_fields)) {
                array_multisort(array_column($dataArr, 'charge'), SORT_DESC, $dataArr);
            }
            // 拼接汇总数据
            if (! empty($dataArr)) {
                if ($is_sum == 1) {
                    $dataArrs = [];
                    foreach ($dataArr as $val) {
                        if ($val['time'] == $sendTime) {
                            $dataArrs[] = $val;
                        }
                    }
                    $totalArr['charge'] = ! empty($totalArr['charge']) ? $totalArr['charge'] : 0;
                    $totalArr['show'] = ! empty($totalArr['show']) ? $totalArr['show'] : 0;
                    $totalArr['photo_click'] = ! empty($totalArr['photo_click']) ? $totalArr['photo_click'] : 0;
                    $totalArr['aclick'] = ! empty($totalArr['aclick']) ? $totalArr['aclick'] : 0;
                    $totalArr['bclick'] = ! empty($totalArr['bclick']) ? $totalArr['bclick'] : 0;
                    $totalArr['photo_click_ratio'] = ! empty($totalArr['photo_click_ratio']) ? $totalArr['photo_click_ratio'] : 0;
                    $totalArr['aclick_ratio'] = ! empty($totalArr['aclick_ratio']) ? $totalArr['aclick_ratio'] : 0;
                    $totalArr['cash_cost'] = ! empty($totalArr['cash_cost']) ? $totalArr['cash_cost'] : 0;
                    $totalArr['folding_convert'] = ! empty($totalArr['folding_convert']) ? $totalArr['folding_convert'] : 0;
                    $totalArr['yesterday_cost_ratio'] = ! empty($totalArr['yesterday_cost_ratio']) ? $totalArr['yesterday_cost_ratio'] : 0;
                    $totalArr['yesterday_cost'] = ! empty($totalArr['yesterday_cost']) ? $totalArr['yesterday_cost'] : 0;
                    $totalArr['show_1k_cost'] = ! empty($totalArr['show_1k_cost']) ? $totalArr['show_1k_cost'] : 0;
                    $totalArr['aclick_1k_cost'] = ! empty($totalArr['aclick_1k_cost']) ? $totalArr['aclick_1k_cost'] : 0;
                    $totalArr['photo_click_cost'] = ! empty($totalArr['photo_click_cost']) ? $totalArr['photo_click_cost'] : 0;
                    $totalArr['action_cost'] = ! empty($totalArr['action_cost']) ? $totalArr['action_cost'] : 0;
                    $totalArr['download_started'] = ! empty($totalArr['download_started']) ? $totalArr['download_started'] : 0;
                    $totalArr['download_completed'] = ! empty($totalArr['download_completed']) ? $totalArr['download_completed'] : 0;
                    $totalArr['download_completed_cost'] = ! empty($totalArr['download_completed_cost']) ? $totalArr['download_completed_cost'] : 0;
                    $totalArr['download_completed_ratio'] = ! empty($totalArr['download_completed_ratio']) ? $totalArr['download_completed_ratio'] : 0;
                    $totalArr['download_started_cost'] = ! empty($totalArr['download_started_cost']) ? $totalArr['download_started_cost'] : 0;
                    $totalArr['download_started_ratio'] = ! empty($totalArr['download_started_ratio']) ? $totalArr['download_started_ratio'] : 0;
                    $totalArr['key_action'] = ! empty($totalArr['key_action']) ? $totalArr['key_action'] : 0;
                    $totalArr['key_action_cost'] = ! empty($totalArr['key_action_cost']) ? $totalArr['key_action_cost'] : 0;
                    $totalArr['key_action_ratio'] = ! empty($totalArr['key_action_ratio']) ? $totalArr['key_action_ratio'] : 0;
                    $totalArr['activation'] = ! empty($totalArr['activation']) ? $totalArr['activation'] : 0;
                    $totalArr['conversion_cost'] = ! empty($totalArr['conversion_cost']) ? $totalArr['conversion_cost'] : 0;
                    $totalArr['download_conversion_ratio'] = ! empty($totalArr['download_conversion_ratio']) ? $totalArr['download_conversion_ratio'] : 0;
                    $totalArr['event_new_user_pay'] = ! empty($totalArr['event_new_user_pay']) ? $totalArr['event_new_user_pay'] : 0;
                    $totalArr['event_new_user_pay_cost'] = ! empty($totalArr['event_new_user_pay_cost']) ? $totalArr['event_new_user_pay_cost'] : 0;
                    $totalArr['event_new_user_pay_ratio'] = ! empty($totalArr['event_new_user_pay_ratio']) ? $totalArr['event_new_user_pay_ratio'] : 0;
                    $totalArr['event_new_user_pay_cost_yesterday'] = ! empty($totalArr['event_new_user_pay_cost_yesterday']) ? $totalArr['event_new_user_pay_cost_yesterday'] : 0;
                    $totalArr['event_register'] = ! empty($totalArr['event_register']) ? $totalArr['event_register'] : 0;
                    $totalArr['event_register_cost'] = ! empty($totalArr['event_register_cost']) ? $totalArr['event_register_cost'] : 0;
                    $totalArr['event_register_ratio'] = ! empty($totalArr['event_register_ratio']) ? $totalArr['event_register_ratio'] : 0;
                    $totalArr['event_pay_first_day'] = ! empty($totalArr['event_pay_first_day']) ? $totalArr['event_pay_first_day'] : 0;
                    $totalArr['event_pay_purchase_amount_first_day'] = ! empty($totalArr['event_pay_purchase_amount_first_day']) ? $totalArr['event_pay_purchase_amount_first_day'] : 0;
                    $totalArr['event_pay_first_day_cost'] = ! empty($totalArr['event_pay_first_day_cost']) ? $totalArr['event_pay_first_day_cost'] : 0;
                    $totalArr['event_pay_first_day_roi'] = ! empty($totalArr['event_pay_first_day_roi']) ? $totalArr['event_pay_first_day_roi'] : 0;
                    $totalArr['event_pay'] = ! empty($totalArr['event_pay']) ? $totalArr['event_pay'] : 0;
                    $totalArr['event_pay_purchase_amount'] = ! empty($totalArr['event_pay_purchase_amount']) ? $totalArr['event_pay_purchase_amount'] : 0;
                    $totalArr['event_pay_cost'] = ! empty($totalArr['event_pay_cost']) ? $totalArr['event_pay_cost'] : 0;
                    $totalArr['event_pay_roi'] = ! empty($totalArr['event_pay_roi']) ? $totalArr['event_pay_roi'] : 0;
                    $totalArr['event_next_day_stay'] = ! empty($totalArr['event_next_day_stay']) ? $totalArr['event_next_day_stay'] : 0;
                    $totalArr['event_next_day_stay_cost'] = ! empty($totalArr['event_next_day_stay_cost']) ? $totalArr['event_next_day_stay_cost'] : 0;
                    $totalArr['event_next_day_stay_ratio'] = ! empty($totalArr['event_next_day_stay_ratio']) ? $totalArr['event_next_day_stay_ratio'] : 0;
                    $totalArr['yesterday_event_next_day_stay'] = ! empty($totalArr['yesterday_event_next_day_stay']) ? $totalArr['yesterday_event_next_day_stay'] : 0;
                    $totalArr['yesterday_event_next_day_stay_cost'] = ! empty($totalArr['yesterday_event_next_day_stay_cost']) ? $totalArr['yesterday_event_next_day_stay_cost'] : 0;
                    $totalArr['yesterday_event_next_day_stay_ratio'] = ! empty($totalArr['yesterday_event_next_day_stay_ratio']) ? $totalArr['yesterday_event_next_day_stay_ratio'] : 0;
                    $totalArr['event_jin_jian_app'] = ! empty($totalArr['event_jin_jian_app']) ? $totalArr['event_jin_jian_app'] : 0;
                    $totalArr['event_jin_jian_app_cost'] = ! empty($totalArr['event_jin_jian_app_cost']) ? $totalArr['event_jin_jian_app_cost'] : 0;
                    $totalArr['form_count'] = ! empty($totalArr['form_count']) ? $totalArr['form_count'] : 0;
                    $totalArr['event_wechat_connected'] = ! empty($totalArr['event_wechat_connected']) ? $totalArr['event_wechat_connected'] : 0;
                    $totalArr['form_cost'] = ! empty($totalArr['form_cost']) ? $totalArr['form_cost'] : 0;
                    $totalArr['form_action_ratio'] = ! empty($totalArr['form_action_ratio']) ? $totalArr['form_action_ratio'] : 0;
                    $totalArr['event_valid_clues'] = ! empty($totalArr['event_valid_clues']) ? $totalArr['event_valid_clues'] : 0;
                    $totalArr['event_valid_clues_cost'] = ! empty($totalArr['event_valid_clues_cost']) ? $totalArr['event_valid_clues_cost'] : 0;
                    $totalArr['event_new_user_jinjian_app'] = ! empty($totalArr['event_new_user_jinjian_app']) ? $totalArr['event_new_user_jinjian_app'] : 0;
                    $totalArr['event_new_user_jinjian_app_cost'] = ! empty($totalArr['event_new_user_jinjian_app_cost']) ? $totalArr['event_new_user_jinjian_app_cost'] : 0;
                    $totalArr['event_new_user_jinjian_app_roi'] = ! empty($totalArr['event_new_user_jinjian_app_roi']) ? $totalArr['event_new_user_jinjian_app_roi'] : 0;
                    $totalArr['event_new_user_credit_grant_app'] = ! empty($totalArr['event_new_user_credit_grant_app']) ? $totalArr['event_new_user_credit_grant_app'] : 0;
                    $totalArr['event_new_user_credit_grant_app_cost'] = ! empty($totalArr['event_new_user_credit_grant_app_cost']) ? $totalArr['event_new_user_credit_grant_app_cost'] : 0;
                    $totalArr['event_new_user_credit_grant_app_roi'] = ! empty($totalArr['event_new_user_credit_grant_app_roi']) ? $totalArr['event_new_user_credit_grant_app_roi'] : 0;
                    $totalArr['event_credit_grant_first_day_app'] = ! empty($totalArr['event_credit_grant_first_day_app']) ? $totalArr['event_credit_grant_first_day_app'] : 0;
                    $totalArr['event_order_paid_roi'] = ! empty($totalArr['event_order_paid_roi']) ? $totalArr['event_order_paid_roi'] : 0;
                    $totalArr['event_order_submit'] = ! empty($totalArr['event_order_submit']) ? $totalArr['event_order_submit'] : 0;
                    $totalArr['event_add_shopping_cart'] = ! empty($totalArr['event_add_shopping_cart']) ? $totalArr['event_add_shopping_cart'] : 0;
                    $totalArr['event_add_shopping_cart_cost'] = ! empty($totalArr['event_add_shopping_cart_cost']) ? $totalArr['event_add_shopping_cart_cost'] : 0;
                    $totalArr['event_watch_app_ad'] = ! empty($totalArr['event_watch_app_ad']) ? $totalArr['event_watch_app_ad'] : 0;
                    $totalArr['event_ad_watch_times'] = ! empty($totalArr['event_ad_watch_times']) ? $totalArr['event_ad_watch_times'] : 0;
                    $totalArr['event_ad_watch_times_ratio'] = ! empty($totalArr['event_ad_watch_times_ratio']) ? $totalArr['event_ad_watch_times_ratio'] : 0;
                    $totalArr['event_ad_watch_times_cost'] = ! empty($totalArr['event_ad_watch_times_cost']) ? $totalArr['event_ad_watch_times_cost'] : 0;
                    $totalArr['event_credit_grant_app'] = ! empty($totalArr['event_credit_grant_app']) ? $totalArr['event_credit_grant_app'] : 0;
                    $totalArr['event_credit_grant_app_cost'] = ! empty($totalArr['event_credit_grant_app_cost']) ? $totalArr['event_credit_grant_app_cost'] : 0;
                    $totalArr['event_credit_grant_app_ratio'] = ! empty($totalArr['event_credit_grant_app_ratio']) ? $totalArr['event_credit_grant_app_ratio'] : 0;

                    $totalArr['event_app_invoked'] = ! empty($totalArr['event_app_invoked']) ? $totalArr['event_app_invoked'] : 0; // 唤醒应用数
                    $totalArr['event_app_invoked_cost'] = ! empty($totalArr['event_app_invoked_cost']) ? $totalArr['event_app_invoked_cost'] : 0; // 唤醒应用成本
                    $totalArr['event_app_invoked_ratio'] = ! empty($totalArr['event_app_invoked_ratio']) ? $totalArr['event_app_invoked_ratio'] : 0; // 唤醒应用率 event_app_invoked / bclick

                    $totalArr['conversion_num_by_impression_7d'] = ! empty($totalArr['conversion_num_by_impression_7d']) ? $totalArr['conversion_num_by_impression_7d'] : 0; // 转化数(计费时间)
                    // 总花费
                    if (in_array('charge', $add_fields) || in_array('event_next_day_stay', $add_fields) || in_array('event_next_day_stay_cost', $add_fields) || in_array('event_next_day_stay_ratio', $add_fields)) {
                        $totalArr['charge'] = array_sum(array_column($dataArrs, 'charge'));
                    }
                    // 封面曝光数
                    if (in_array('show', $add_fields)) {
                        $totalArr['show'] = array_sum(array_column($dataArrs, 'show'));
                    }
                    // 封面点击数
                    if (in_array('photo_click', $add_fields)) {
                        $totalArr['photo_click'] = array_sum(array_column($dataArrs, 'photo_click'));
                    }
                    // 素材曝光数
                    if (in_array('aclick_ratio', $add_fields) || in_array('aclick', $add_fields) || in_array('bclick', $add_fields)) {
                        $totalArr['aclick'] = array_sum(array_column($dataArrs, 'aclick'));
                    }
                    // 行为数
                    if (in_array('aclick_ratio', $add_fields) || in_array('aclick', $add_fields) || in_array('bclick', $add_fields)) {
                        $totalArr['bclick'] = array_sum(array_column($dataArrs, 'bclick'));
                    }
                    // 封面点击率
                    if (in_array('photo_click_ratio', $add_fields)) {
                        $totalArr['photo_click_ratio'] = ! empty($totalArr['show']) ? round($totalArr['photo_click'] / $totalArr['show'] * 100, 2) . '%' : 0;
                    }
                    // 素材点击率
                    if (in_array('aclick_ratio', $add_fields) || in_array('aclick', $add_fields) || in_array('bclick', $add_fields)) {
                        $totalArr['aclick_ratio'] = ! empty($totalArr['aclick']) ? round($totalArr['bclick'] / $totalArr['aclick'] * 100, 2) . '%' : 0;
                    }
                    // 折后消耗
                    if (in_array('cash_cost', $add_fields)) {
                        $totalArr['cash_cost'] = array_sum(array_column($dataArrs, 'cash_cost'));
                    }
                    // 折后成本
                    if (in_array('folding_convert', $add_fields)) {
                        $totalArr['folding_convert'] = array_sum(array_column($dataArrs, 'folding_convert'));
                    }
                    // 昨日同时段消耗
                    if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_ratio', $add_fields)) {
                        $totalArr['yesterday_cost'] = array_sum(array_column($dataArrs, 'yesterday_cost'));
                    }
                    // 昨日同时段消耗环比
                    if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_ratio', $add_fields)) {
                        $x = bcsub(strval($totalArr['charge']), strval($totalArr['yesterday_cost']), 3);
                        $totalArr['yesterday_cost_ratio'] = ! empty($totalArr['yesterday_cost']) ? round($x / $totalArr['yesterday_cost'] * 100, 2) . '%' : 0;
                    }
                    // 平均千次封面曝光花费
                    if (in_array('show_1k_cost', $add_fields)) {
                        $totalArr['show'] = array_sum(array_column($dataArrs, 'show'));
                        $totalArr['show_1k_cost'] = ! empty($totalArr['show']) ? round($totalArr['charge'] / $totalArr['show'] * 1000, 3) : 0;
                    }
                    // 平均千次素材曝光花费
                    if (in_array('aclick_1k_cost', $add_fields)) {
                        $totalArr['aclick'] = array_sum(array_column($dataArrs, 'aclick'));
                        $totalArr['aclick_1k_cost'] = ! empty($totalArr['aclick']) ? round($totalArr['charge'] / $totalArr['aclick'] * 1000, 3) : 0;
                    }
                    // 平均点击单价
                    if (in_array('photo_click_cost', $add_fields)) {
                        $totalArr['photo_click'] = array_sum(array_column($dataArrs, 'photo_click'));
                        $totalArr['photo_click_cost'] = ! empty($totalArr['photo_click']) ? round($totalArr['charge'] / $totalArr['photo_click'], 3) : 0;
                    }
                    // 平均行为单价
                    if (in_array('action_cost', $add_fields)) {
                        $totalArr['action_cost'] = ! empty($totalArr['bclick']) ? round($totalArr['charge'] / $totalArr['bclick'], 3) : 0;
                    }
                    // 安卓下载开始数
                    if (in_array('download_started', $add_fields)) {
                        $totalArr['download_started'] = array_sum(array_column($dataArrs, 'download_started'));
                    }
                    // 安卓下载开始单价
                    if (in_array('download_started_cost', $add_fields)) {
                        $totalArr['download_started_cost'] = ! empty($totalArr['download_started']) ? round($totalArr['charge'] / $totalArr['download_started'], 3) : 0;
                    }
                    // 安卓下载开始率
                    if (in_array('download_started_ratio', $add_fields)) {
                        $totalArr['download_started_ratio'] = ! empty($totalArr['bclick']) ? round($totalArr['download_started'] / $totalArr['bclick'] * 100, 2) . '%' : 0;
                    }
                    // 安卓下载完成数
                    if (in_array('download_completed', $add_fields)) {
                        $totalArr['download_completed'] = array_sum(array_column($dataArrs, 'download_completed'));
                    }
                    // 安卓下载完成单价
                    if (in_array('download_completed_cost', $add_fields)) {
                        $totalArr['download_completed_cost'] = ! empty($totalArr['download_completed']) ? round($totalArr['charge'] / $totalArr['download_completed'], 3) : 0;
                    }
                    // 安卓下载完成率
                    if (in_array('download_completed_ratio', $add_fields)) {
                        $totalArr['download_completed_ratio'] = ! empty($totalArr['download_started']) ? round($totalArr['download_completed'] / $totalArr['download_started'] * 100, 2) . '%' : 0;
                    }
                    // 激活数
                    if (in_array('activation', $add_fields) || in_array('event_next_day_stay', $add_fields) || in_array('event_next_day_stay_cost', $add_fields) || in_array('event_next_day_stay_ratio', $add_fields)) {
                        $totalArr['activation'] = array_sum(array_column($dataArrs, 'activation'));
                    }
                    // 激活单价
                    if (in_array('conversion_cost', $add_fields)) {
                        $totalArr['conversion_cost'] = ! empty($totalArr['activation']) ? round($totalArr['charge'] / $totalArr['activation'], 3) : 0;
                    }
                    // 关键行为数
                    if (in_array('key_action', $add_fields)) {
                        $totalArr['key_action'] = array_sum(array_column($dataArrs, 'key_action'));
                    }
                    // 关键行为成本
                    if (in_array('key_action_cost', $add_fields)) {
                        $totalArr['key_action_cost'] = ! empty($totalArr['key_action']) ? round($totalArr['charge'] / $totalArr['key_action'], 3) : 0;
                    }
                    // 关键行为率
                    if (in_array('key_action_ratio', $add_fields)) {
                        $totalArr['key_action_ratio'] = ! empty($totalArr['activation']) ? round($totalArr['key_action'] / $totalArr['activation'] * 100, 2) . '%' : 0;
                    }
                    // 下载完成激活率
                    if (in_array('download_conversion_ratio', $add_fields)) {
                        $totalArr['download_conversion_ratio'] = ! empty($totalArr['download_completed']) ? round($totalArr['activation'] / $totalArr['download_completed'] * 100, 2) . '%' : 0;
                    }
                    // 注册数
                    if (in_array('event_register', $add_fields)) {
                        $totalArr['event_register'] = array_sum(array_column($dataArrs, 'event_register'));
                    }
                    // 注册成本
                    if (in_array('event_register_cost', $add_fields)) {
                        $totalArr['event_register_cost'] = ! empty($totalArr['event_register']) ? round($totalArr['charge'] / $totalArr['event_register'], 3) : 0;
                    }
                    // 注册率
                    if (in_array('event_register_ratio', $add_fields)) {
                        $totalArr['event_register_ratio'] = ! empty($totalArr['activation']) ? round($totalArr['event_register'] / $totalArr['activation'] * 100, 2) . '%' : 0;
                    }
                    // 首日付费次数
                    if (in_array('event_pay_first_day', $add_fields)) {
                        $totalArr['event_pay_first_day'] = array_sum(array_column($dataArrs, 'event_pay_first_day'));
                    }
                    // 首日付费金额
                    if (in_array('event_pay_purchase_amount_first_day', $add_fields)) {
                        $totalArr['event_pay_purchase_amount_first_day'] = array_sum(array_column($dataArrs, 'event_pay_purchase_amount_first_day'));
                    }
                    // 首日付费次数成本
                    if (in_array('event_pay_first_day_cost', $add_fields)) {
                        $totalArr['event_pay_first_day_cost'] = ! empty($totalArr['event_pay_first_day']) ? round($totalArr['charge'] / $totalArr['event_pay_first_day'], 3) : 0;
                    }
                    // 首日ROI
                    if (in_array('event_pay_first_day_roi', $add_fields)) {
                        $totalArr['event_pay_first_day_roi'] = array_sum(array_column($dataArrs, 'event_pay_first_day_roi'));
                    }
                    // 付费次数
                    if (in_array('event_pay', $add_fields)) {
                        $totalArr['event_pay'] = array_sum(array_column($dataArrs, 'event_pay'));
                    }
                    // 付费金额
                    if (in_array('event_pay_purchase_amount', $add_fields)) {
                        $totalArr['event_pay_purchase_amount'] = array_sum(array_column($dataArrs, 'event_pay_purchase_amount'));
                    }
                    // 付费次数成本
                    if (in_array('event_pay_cost', $add_fields)) {
                        $totalArr['event_pay_cost'] = ! empty($totalArr['event_pay']) ? round($totalArr['charge'] / $totalArr['event_pay'], 3) : 0;
                    }
                    // ROI
                    if (in_array('event_pay_roi', $add_fields)) {
                        $totalArr['event_pay_roi'] = ! empty($totalArr['charge']) ? round($totalArr['event_pay_purchase_amount'] / $totalArr['charge'], 3) : 0;
                    }

                    // 次日留存数
                    if (in_array('event_next_day_stay', $add_fields) || in_array('event_next_day_stay_cost', $add_fields) || in_array('event_next_day_stay_ratio', $add_fields)) {
                        $totalArr['event_next_day_stay'] = array_sum($ciDayEventNextDayStayArr);
                    }
                    // 次日留村成本
                    if (in_array('event_next_day_stay', $add_fields) || in_array('event_next_day_stay_cost', $add_fields) || in_array('event_next_day_stay_ratio', $add_fields)) {
                        $ciDayChargeSum = array_sum($ciDayChargeArr);
                        $totalArr['event_next_day_stay_cost'] = ! empty($totalArr['event_next_day_stay']) ? round($ciDayChargeSum / $totalArr['event_next_day_stay'], 3) : 0;
                    }
                    // 次留率
                    if (in_array('event_next_day_stay', $add_fields) || in_array('event_next_day_stay_cost', $add_fields) || in_array('event_next_day_stay_ratio', $add_fields)) {
                        $ciDayActivationSum = array_sum($ciDayActivationArr);
                        $totalArr['event_next_day_stay_ratio'] = ! empty($ciDayActivationSum) ? round($totalArr['event_next_day_stay'] / $ciDayActivationSum * 100, 2) . '%' : 0;
                    }

                    // 昨日次留数
                    if (in_array('yesterday_event_next_day_stay', $add_fields) || in_array('yesterday_event_next_day_stay_cost', $add_fields) || in_array('yesterday_event_next_day_stay_ratio', $add_fields)) {
                        $totalArr['yesterday_event_next_day_stay'] = array_sum($yesterdayEventNextDayStayArr);
                    }
                    // 昨日次留成本
                    if (in_array('yesterday_event_next_day_stay', $add_fields) || in_array('yesterday_event_next_day_stay_cost', $add_fields) || in_array('yesterday_event_next_day_stay_ratio', $add_fields)) {
                        $yesterdayChargeSum = array_sum($yesterdayChargeArr);
                        $totalArr['yesterday_event_next_day_stay_cost'] = ! empty($totalArr['yesterday_event_next_day_stay']) ? round($yesterdayChargeSum / $totalArr['yesterday_event_next_day_stay'], 3) : 0;
                    }
                    // 昨日次留率
                    if (in_array('yesterday_event_next_day_stay', $add_fields) || in_array('yesterday_event_next_day_stay_cost', $add_fields) || in_array('yesterday_event_next_day_stay_ratio', $add_fields)) {
                        $yesterdayActivationSum = array_sum($yesterdayActivationArr);
                        $totalArr['yesterday_event_next_day_stay_ratio'] = ! empty($yesterdayActivationSum) ? round($totalArr['yesterday_event_next_day_stay'] / $yesterdayActivationSum * 100, 2) . '%' : 0;
                    }
                    // 完件数
                    if (in_array('event_jin_jian_app', $add_fields)) {
                        $totalArr['event_jin_jian_app'] = array_sum(array_column($dataArrs, 'event_jin_jian_app'));
                    }
                    // 完件成本
                    if (in_array('event_jin_jian_app_cost', $add_fields)) {
                        $totalArr['event_jin_jian_app_cost'] = ! empty($totalArr['event_jin_jian_app']) ? round($totalArr['charge'] / $totalArr['event_jin_jian_app'], 3) : 0;
                    }
                    // 表单提交数
                    if (in_array('form_count', $add_fields)) {
                        $totalArr['form_count'] = array_sum(array_column($dataArrs, 'form_count'));
                    }
                    // 微信加粉数
                    if (in_array('event_wechat_connected', $add_fields)) {
                        $totalArr['event_wechat_connected'] = array_sum(array_column($dataArrs, 'event_wechat_connected'));
                    }
                    // 表单提交单价
                    if (in_array('form_cost', $add_fields)) {
                        $totalArr['form_cost'] = ! empty($totalArr['form_count']) ? round($totalArr['charge'] / $totalArr['form_count'], 3) : 0;
                    }
                    // 表单提交点击率
                    if (in_array('form_action_ratio', $add_fields)) {
                        $totalArr['form_action_ratio'] = ! empty($totalArr['activation']) ? round($totalArr['form_count'] / $totalArr['activation'] * 100, 2) . '%' : 0;
                    }
                    // 新增付费人数
                    if (in_array('event_new_user_pay', $add_fields)) {
                        $totalArr['event_new_user_pay'] = array_sum(array_column($dataArrs, 'event_new_user_pay'));
                    }
                    // 新增付费人数成本
                    if (in_array('event_new_user_pay_cost', $add_fields)) {
                        $totalArr['event_new_user_pay_cost'] = ! empty($totalArr['event_new_user_pay']) ? round($totalArr['charge'] / $totalArr['event_new_user_pay'], 3) : 0;
                    }
                    // 新增付费人数率
                    if (in_array('event_new_user_pay_ratio', $add_fields)) {
                        $h = $totalArr['activation'] + $totalArr['form_count'];
                        $totalArr['event_new_user_pay_ratio'] = ! empty($h) ? round($totalArr['event_new_user_pay'] / $h * 100, 2) . '%' : 0;
                    }
                    // 付费成本环比
                    if (in_array('event_new_user_pay_ringgit', $add_fields)) {
                        $totalArr['event_new_user_pay_ringgit'] = ! empty($totalArr['event_new_user_pay_cost_yesterday']) ? round($totalArr['event_new_user_pay_cost'] / $totalArr['event_new_user_pay_cost_yesterday'], 3) : 0;
                    }
                    // 有效线索数
                    if (in_array('event_valid_clues', $add_fields)) {
                        $totalArr['event_valid_clues'] = array_sum(array_column($dataArrs, 'event_valid_clues'));
                    }
                    // 有效线索成本
                    if (in_array('event_valid_clues_cost', $add_fields)) {
                        $totalArr['event_valid_clues_cost'] = ! empty($totalArr['event_valid_clues']) ? round($totalArr['charge'] / $totalArr['event_valid_clues'], 3) : 0;
                    }
                    // 新增完件人数
                    if (in_array('event_new_user_jinjian_app', $add_fields)) {
                        $totalArr['event_new_user_jinjian_app'] = array_sum(array_column($dataArrs, 'event_new_user_jinjian_app'));
                    }
                    // 新增完件人数成本
                    if (in_array('event_new_user_jinjian_app_cost', $add_fields)) {
                        $totalArr['event_new_user_jinjian_app_cost'] = ! empty($totalArr['event_new_user_jinjian_app']) ? round($totalArr['charge'] / $totalArr['event_new_user_jinjian_app'], 3) : 0;
                    }
                    // 新增完件人数率
                    if (in_array('event_new_user_jinjian_app_roi', $add_fields)) {
                        $h = $totalArr['activation'] + $totalArr['form_count'];
                        $totalArr['event_new_user_jinjian_app_roi'] = ! empty($h) ? round($totalArr['event_new_user_jinjian_app'] / $h * 100, 2) . '%' : 0;
                    }
                    // 授信数
                    if (in_array('event_credit_grant_app', $add_fields)) {
                        $totalArr['event_credit_grant_app'] = array_sum(array_column($dataArrs, 'event_credit_grant_app'));
                    }
                    // 授信成本
                    if (in_array('event_credit_grant_app_cost', $add_fields)) {
                        $totalArr['event_credit_grant_app_cost'] = ! empty($totalArr['event_credit_grant_app']) ? round($totalArr['charge'] / $totalArr['event_credit_grant_app'], 3) : 0;
                    }
                    // 授信率
                    if (in_array('event_credit_grant_app_ratio', $add_fields)) {
                        $totalArr['event_credit_grant_app_ratio'] = ! empty($totalArr['event_jin_jian_app']) ? round($totalArr['event_credit_grant_app'] / $totalArr['event_jin_jian_app'] * 100, 2) . '%' : 0;
                    }
                    // 新增授信人数
                    if (in_array('event_new_user_credit_grant_app', $add_fields)) {
                        $totalArr['event_new_user_credit_grant_app'] = array_sum(array_column($dataArrs, 'event_new_user_credit_grant_app'));
                    }
                    // 新增授信人数成本
                    if (in_array('event_new_user_credit_grant_app_cost', $add_fields)) {
                        $totalArr['event_new_user_credit_grant_app_cost'] = ! empty($totalArr['event_new_user_credit_grant_app']) ? round($totalArr['charge'] / $totalArr['event_new_user_credit_grant_app'], 3) : 0;
                    }
                    // 新增授信人数率
                    if (in_array('event_new_user_credit_grant_app_roi', $add_fields)) {
                        $h = $totalArr['activation'] + $totalArr['form_count'];
                        $totalArr['event_new_user_credit_grant_app_roi'] = ! empty($h) ? round($totalArr['event_new_user_credit_grant_app'] / $h * 100, 2) . '%' : 0;
                    }
                    // app首日授信数
                    if (in_array('event_credit_grant_first_day_app', $add_fields)) {
                        $totalArr['event_credit_grant_first_day_app'] = array_sum(array_column($dataArrs, 'event_credit_grant_first_day_app'));
                    }
                    // 提交订单数
                    if (in_array('event_order_submit', $add_fields)) {
                        $totalArr['event_order_submit'] = array_sum(array_column($dataArrs, 'event_order_submit'));
                    }
                    // 订单支付率
                    if (in_array('event_order_paid_roi', $add_fields)) {
                        $totalArr['event_order_paid_roi'] = 0;
                    }
                    // 添加购物车数
                    if (in_array('event_add_shopping_cart', $add_fields)) {
                        $totalArr['event_add_shopping_cart'] = array_sum(array_column($dataArrs, 'event_add_shopping_cart'));
                    }
                    // 添加购物车成本
                    if (in_array('event_add_shopping_cart_cost', $add_fields)) {
                        $totalArr['event_add_shopping_cart_cost'] = ! empty($totalArr['event_add_shopping_cart']) ? round($totalArr['charge'] / $totalArr['event_add_shopping_cart'], 3) : 0;
                    }
                    // 广告观看
                    if (in_array('event_watch_app_ad', $add_fields)) {
                        $totalArr['event_watch_app_ad'] = array_sum(array_column($dataArrs, 'event_watch_app_ad'));
                    }
                    // 广告观看次数
                    if (in_array('event_ad_watch_times', $add_fields)) {
                        $totalArr['event_ad_watch_times'] = array_sum(array_column($dataArrs, 'event_ad_watch_times'));
                    }
                    // 广告观看次数成本
                    if (in_array('event_ad_watch_times_cost', $add_fields)) {
                        $totalArr['event_ad_watch_times_cost'] = ! empty($totalArr['event_ad_watch_times']) ? round($totalArr['charge'] / $totalArr['event_ad_watch_times'], 3) : 0;
                    }
                    // 广告观看次数转化率
                    if (in_array('event_ad_watch_times_ratio', $add_fields)) {
                        $totalArr['event_ad_watch_times_ratio'] = ! empty($totalArr['activation']) ? round($totalArr['event_ad_watch_times'] / $totalArr['activation'] * 100, 2) . '%' : 0;
                    }
                    // 唤醒应用数
                    if (in_array('event_app_invoked', $add_fields)) {
                        $totalArr['event_app_invoked'] = array_sum(array_column($dataArrs, 'event_app_invoked'));
                    }
                    // 唤醒应用成本
                    if (in_array('event_app_invoked_cost', $add_fields)) {
                        $totalArr['event_app_invoked_cost'] = array_sum(array_column($dataArrs, 'event_app_invoked_cost'));
                    }
                    // 唤醒应用率
                    if (in_array('event_app_invoked_ratio', $add_fields)) {
                        $totalArr['event_app_invoked_ratio'] = ! empty($totalArr['bclick']) ? round($totalArr['event_app_invoked'] / $totalArr['bclick'] * 100, 2) . '%' : 0;
                    }
                    // 转化数(计费时间)
                    if (in_array('conversion_num_by_impression_7d', $add_fields)) {
                        $totalArr['conversion_num_by_impression_7d'] = array_sum(array_column($dataArrs, 'conversion_num_by_impression_7d'));
                    }
                    // 自定义数据
                    if (! empty($custom_fields_name)) {
                        foreach ($custom_fields_name as $cfnv) {
                            $molecule = ! empty($totalArr[$cfnv['molecule']]) ? str_replace('%', '', $totalArr[$cfnv['molecule']]) : 0;
                            $denominator = ! empty($totalArr[$cfnv['denominator']]) ? str_replace('%', '', $totalArr[$cfnv['denominator']]) : 0;
                            $totalArr[$cfnv['field']] = $denominator > 0 ? round($molecule / $denominator, 2) : 0;
                        }
                    }
                }
            }
            $result['data'] = $dataArr;
            $result['total'] = $totalArr;
            // 返回信息
            return $result;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    // 获取广告主
    public static function importAccountReport(int $statDate = 0, string $advertiserId = '', int $dateType = 0, int $statHour = 0)
    {
        try {
            $data = (new ReportRecordPushKsAccountModel())
                ->where('advertiser_id', $advertiserId)
                ->where('stat_date', $statDate)
                ->where('stat_hour', '<=', $statHour)
                ->where('date_type', $dateType)
                ->order('stat_hour ASC')
                ->select();
            $datas = [];
            foreach ($data as $v) {
                $datas[] = json_decode($v['data_content'], true);
            }
            return $datas;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    // 获取计划
    public static function importCampaignReport(int $statDate = 0, string $advertiserId = '', int $dateType = 1)
    {
        $where = [
            'stat_date' => $statDate,
            'advertiser_id' => $advertiserId,
            'date_type' => $dateType,
        ];
        try {
            $data = (new ReportRecordPushKsCampaignModel())->where($where)->column('data_content');
            $datas = [];
            foreach ($data as $v) {
                $datas[] = json_decode($v, true);
            }
            return $datas;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    // 获取广告组
    public static function importUnitReport(int $statDate = 0, string $advertiserId = '', int $dateType = 1)
    {
        $where = [
            'stat_date' => $statDate,
            'advertiser_id' => $advertiserId,
            'date_type' => $dateType,
        ];
        try {
            $data = (new ReportRecordPushKsUnitModel())->where($where)->column('data_content');
            $datas = [];
            foreach ($data as $v) {
                $datas[] = json_decode($v, true);
            }
            return $datas;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * 获取消耗的总和.
     * @return int|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getAccountCost(string $advertiserId = '', int $statDate = 0, int $statHour = 0, int $dateType = 0, string $field = '')
    {
        $data = (new ReportRecordPushKsAccountModel())
            ->where('advertiser_id', $advertiserId)
            ->where('stat_date', $statDate)
            ->where('stat_hour', '<=', $statHour)
            ->where('date_type', $dateType)
            ->select();
        $sum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $sum = $val['data_content'][$field] + $sum;
            }
        }
        return $sum;
    }

    /**
     * 获取消耗的总和.
     * @return int|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getUnitCost(string $advertiserId = '', string $id = '', int $statDate = 0, int $statHour = 0, int $dateType = 0, string $field = '')
    {
        $data = (new ReportRecordPushKsUnitModel())->where('advertiser_id', $advertiserId)
            ->where('unit_id', $id)
            ->where('stat_date', $statDate)
            ->where('stat_hour', $statHour)
            ->where('date_type', $dateType)
            ->select();
        $sum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $sum = $val['data_content'][$field] + $sum;
            }
        }
        return $sum;
    }

    /**
     * 获取消耗的总和.
     * @return int|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getCampaignCost(string $advertiserId = '', string $id = '', int $statDate = 0, int $statHour = 0, int $dateType = 0, string $field = '')
    {
        $data = (new ReportRecordPushKsCampaignModel())
            ->where('advertiser_id', $advertiserId)
            ->where('campaign_id', $id)
            ->where('stat_date', $statDate)
            ->where('stat_hour', '<=', $statHour)
            ->where('date_type', $dateType)
            ->select();
        $sum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $sum = $val['data_content'][$field] + $sum;
            }
        }
        return $sum;
    }

    /**
     * 获取次日次留.
     */
    public static function getAccountSecondaryStation(string $advertiserId = '', int $statDate = 0, int $statHour = 0, int $report_date_type = 0): array
    {
        $data = (new ReportRecordPushKsAccountModel())
            ->where('advertiser_id', $advertiserId)
            ->where('stat_date', $statDate)
            ->where('date_type', $report_date_type)
            ->select();
        $eventNextDayStaySum = 0;
        $chargeSum = 0;
        $activationSum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $chargeSum += $val['data_content']['charge'];
                $activationSum += $val['data_content']['activation'];
                if ($statHour >= $val['stat_hour']) {
                    $eventNextDayStaySum += $val['data_content']['event_next_day_stay'];
                }
            }
        }
        return [
            'event_next_day_stay' => $eventNextDayStaySum,
            'charge' => $chargeSum,
            'activation' => $activationSum,
        ];
    }

    /**
     * 获取次日次留.
     * @return int|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getUnitSecondaryStation(string $advertiserId = '', int $statDate = 0, int $statHour = 0, int $report_date_type = 0): array
    {
        $data = (new ReportRecordPushKsUnitModel())
            ->where('advertiser_id', $advertiserId)
            ->where('stat_date', $statDate)
            ->where('stat_hour', $statHour)
            ->where('date_type', $report_date_type)
            ->select();
        $eventNextDayStaySum = 0;
        $chargeSum = 0;
        $activationSum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $eventNextDayStaySum += $val['data_content']['event_next_day_stay'];
                $chargeSum += $val['data_content']['charge'];
                $activationSum += $val['data_content']['activation'];
            }
        }
        return [
            'event_next_day_stay' => $eventNextDayStaySum,
            'charge' => $chargeSum,
            'activation' => $activationSum,
        ];
    }

    /**
     * 获取次日次留.
     * @return int|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getCampaignSecondaryStation(string $advertiserId = '', int $statDate = 0, int $statHour = 0, int $report_date_type = 0): array
    {
        $data = (new ReportRecordPushKsCampaignModel())
            ->where('advertiser_id', $advertiserId)
            ->where('stat_date', $statDate)
            ->where('stat_hour', $statHour)
            ->where('date_type', $report_date_type)
            ->select();
        $eventNextDayStaySum = 0;
        $chargeSum = 0;
        $activationSum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $eventNextDayStaySum += $val['data_content']['event_next_day_stay'];
                $chargeSum += $val['data_content']['charge'];
                $activationSum += $val['data_content']['activation'];
            }
        }
        return [
            'event_next_day_stay' => $eventNextDayStaySum,
            'charge' => $chargeSum,
            'activation' => $activationSum,
        ];
    }

    /**
     * 获取日报数据 -- 广告主.
     * @param int $statDate 当前日期
     * @param string $advertiserId 账户ID
     * @param int $dateType 时报日报
     * @param int $statHour 发送时间
     * @return array|bool
     */
    public static function importAccountReportDate(int $statDate = 0, string $advertiserId = '', int $dateType = 0, int $statHour = 0)
    {
        $where = [
            'stat_date' => $statDate,
            'advertiser_id' => $advertiserId,
            'date_type' => $dateType,
            'stat_hour' => $statHour,
        ];
        try {
            $data = (new ReportRecordPushKsAccountDateModel())->where($where)->column('data_content');
            $datas = [];
            foreach ($data as $v) {
                $datas[] = json_decode($v, true);
            }
            return $datas;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }
}
